-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[llvm] Add test to compile and run a simple fibonacci program
- Loading branch information
1 parent
fb3882c
commit 545950d
Showing
5 changed files
with
126 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//! Tests that compile code and run it. | ||
use std::path::PathBuf; | ||
|
||
use assert_cmd::Command; | ||
use expect_test::expect; | ||
use pliron::Lazy; | ||
use tempfile::tempdir; | ||
|
||
static RESOURCES_DIR: pliron::Lazy<PathBuf> = Lazy::new(|| { | ||
[env!("CARGO_MANIFEST_DIR"), "tests", "resources"] | ||
.iter() | ||
.collect() | ||
}); | ||
|
||
#[test] | ||
fn test_fib() { | ||
// Create a tempdir() to place the temporary compiled files. | ||
let tmp_dir = tempdir().unwrap(); | ||
|
||
// clang -c -emit-llvm -o $tmp/fib.bc tests/resources/fib.c | ||
let mut cmd = Command::new("clang-17"); | ||
let compile_fib = cmd | ||
.current_dir(&*RESOURCES_DIR) | ||
.args([ | ||
"-c", | ||
"-emit-llvm", | ||
"-o", | ||
tmp_dir.path().join("fib.bc").to_str().unwrap(), | ||
"fib.c", | ||
]) | ||
.output() | ||
.expect("failed to execute clang to compile fib.c"); | ||
assert!( | ||
compile_fib.status.success(), | ||
"{}", | ||
String::from_utf8(compile_fib.stderr).unwrap() | ||
); | ||
|
||
// llvm-opt -S -i $tmp/fib.bc -o $tmp/fib.opt.ll | ||
let mut cmd = Command::cargo_bin("llvm-opt").unwrap(); | ||
let compile_fib = cmd | ||
.current_dir(&*RESOURCES_DIR) | ||
.args([ | ||
"-S", | ||
"-i", | ||
tmp_dir.path().join("fib.bc").to_str().unwrap(), | ||
"-o", | ||
tmp_dir.path().join("fib.out.ll").to_str().unwrap(), | ||
]) | ||
.output() | ||
.expect("failed to execute llvm-opt to compile fib.bc to fib.out.ll"); | ||
assert!( | ||
compile_fib.status.success(), | ||
"{}", | ||
String::from_utf8(compile_fib.stderr).unwrap() | ||
); | ||
|
||
// clang -o $tmp/fib $tmp/fib.out.ll tests/resources/fib-main.c | ||
let mut cmd = Command::new("clang-17"); | ||
let compile_fib = cmd | ||
.current_dir(&*RESOURCES_DIR) | ||
.args([ | ||
"-o", | ||
tmp_dir.path().join("fib").to_str().unwrap(), | ||
tmp_dir.path().join("fib.out.ll").to_str().unwrap(), | ||
"fib-main.c", | ||
]) | ||
.output() | ||
.expect("failed to execute clang to compile fib-main.c"); | ||
assert!( | ||
compile_fib.status.success(), | ||
"{}", | ||
String::from_utf8(compile_fib.stderr).unwrap() | ||
); | ||
|
||
// $tmp/fib | ||
let mut cmd = Command::new(tmp_dir.path().join("fib")); | ||
let run_fib = cmd.output().expect("Error executing compiled fib program"); | ||
assert!(run_fib.status.success()); | ||
let fib_output = String::from_utf8(run_fib.stdout).unwrap(); | ||
expect![[r#" | ||
fib(0): 0 | ||
fib(1): 0 | ||
fib(2): 1 | ||
fib(3): 1 | ||
fib(4): 2 | ||
"#]] | ||
.assert_eq(&fib_output); | ||
} |
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,14 @@ | ||
#include <stdio.h> | ||
|
||
extern int fib(int n); | ||
|
||
int main() { | ||
|
||
printf("fib(0): %d\n", fib(0)); | ||
printf("fib(1): %d\n", fib(1)); | ||
printf("fib(2): %d\n", fib(2)); | ||
printf("fib(3): %d\n", fib(3)); | ||
printf("fib(4): %d\n", fib(4)); | ||
|
||
return 0; | ||
} |
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,19 @@ | ||
int fib(int n) { | ||
if (n == 1) { | ||
return 0; | ||
} | ||
if (n == 2) { | ||
return 1; | ||
} | ||
|
||
int prev2 = 0; | ||
int prev1 = 1; | ||
|
||
int cur; | ||
for (int i = 3; i <= n; i++) { | ||
cur = prev1 + prev2; | ||
prev2 = prev1; | ||
prev1 = cur; | ||
} | ||
return cur; | ||
} |