Update skeleton

This commit is contained in:
Jeehoon Kang
2020-06-05 00:54:00 +09:00
parent 62c5208631
commit 7bb9ed11b5
123 changed files with 49554 additions and 868 deletions

View File

@@ -1,4 +1,3 @@
use lang_c::ast::*;
use std::fs::{self, File};
use std::io::{stderr, Read, Write};
use std::path::Path;
@@ -12,12 +11,18 @@ use crate::*;
// So, we decide KECC sets an exit code of 102 after 101 when the test skipped.
pub const SKIP_TEST: i32 = 102;
pub fn test_write_c(unit: &TranslationUnit, _path: &Path) {
pub fn test_write_c(path: &Path) {
// Check if the file has .c extension
assert_eq!(path.extension(), Some(std::ffi::OsStr::new("c")));
let unit = c::Parse::default()
.translate(&path)
.unwrap_or_else(|_| panic!("parse failed {}", path.display()));
let temp_dir = tempdir().expect("temp dir creation failed");
let temp_file_path = temp_dir.path().join("temp.c");
let mut temp_file = File::create(&temp_file_path).unwrap();
crate::write(unit, &mut temp_file).unwrap();
crate::write(&unit, &mut temp_file).unwrap();
let new_unit = c::Parse::default()
.translate(&temp_file_path.as_path())
@@ -27,9 +32,12 @@ pub fn test_write_c(unit: &TranslationUnit, _path: &Path) {
temp_dir.close().expect("temp dir deletion failed");
}
pub fn test_irgen(unit: &TranslationUnit, path: &Path) {
pub fn test_irgen(path: &Path) {
// Check if the file has .c extension
assert_eq!(path.extension(), Some(std::ffi::OsStr::new("c")));
let unit = c::Parse::default()
.translate(&path)
.unwrap_or_else(|_| panic!("parse failed {}", path.display()));
// Test parse
c::Parse::default()
@@ -93,16 +101,11 @@ pub fn test_irgen(unit: &TranslationUnit, path: &Path) {
let status = some_or_exit!(status.code(), SKIP_TEST);
let ir = match Irgen::default().translate(unit) {
Ok(ir) => ir,
Err(irgen_error) => panic!("{}", irgen_error),
};
let ir = Irgen::default()
.translate(&unit)
.unwrap_or_else(|irgen_error| panic!("{}", irgen_error));
let args = Vec::new();
let result = match ir::interp(&ir, args) {
Ok(result) => result,
Err(interp_error) => panic!("{}", interp_error),
};
let result = ir::interp(&ir, args).unwrap_or_else(|interp_error| panic!("{}", interp_error));
// We only allow main function whose return type is `int`
let (value, width, is_signed) = result.get_int().expect("non-integer value occurs");
assert_eq!(width, 32);
@@ -116,61 +119,36 @@ pub fn test_irgen(unit: &TranslationUnit, path: &Path) {
assert_eq!(status as i8, value as i8);
}
pub fn test_irparse(unit: &TranslationUnit, path: &Path) {
pub fn test_irparse(path: &Path) {
let ignore_list = vec![
"examples/c/array5.c",
"examples/c/foo3.c",
"examples/c/minus_constant.c",
"examples/c/struct.c",
"examples/c/struct2.c",
"examples/c/struct3.c",
"examples/c/temp2.c",
"examples/c/typecast.c",
];
if ignore_list.contains(&path.to_str().expect("`path` must be transformed to `&str`")) {
println!("skip test");
return;
}
// Check if the file has .c extension
assert_eq!(path.extension(), Some(std::ffi::OsStr::new("c")));
let unit = c::Parse::default()
.translate(&path)
.unwrap_or_else(|_| panic!("parse failed {}", path.display()));
// Test parse
c::Parse::default()
.translate(&path)
.expect("failed to parse the given program");
let file_path = path.display().to_string();
let bin_path = path
.with_extension("irparse")
.as_path()
.display()
.to_string();
// Compile c file: If fails, test is vacuously success
if !Command::new("gcc")
.args(&["-O1", &file_path, "-o", &bin_path])
.stderr(Stdio::null())
.status()
.unwrap()
.success()
{
return;
}
// Execute compiled executable
let mut child = Command::new(fs::canonicalize(bin_path.clone()).unwrap())
.spawn()
.expect("failed to execute the compiled executable");
Command::new("rm")
.arg(bin_path)
.status()
.expect("failed to remove compiled executable");
let status = some_or!(
child
.wait_timeout_ms(500)
.expect("failed to obtain exit status from child process"),
{
println!("timeout occurs");
child.kill().unwrap();
child.wait().unwrap();
return;
}
);
let _status = some_or!(status.code(), return);
let ir = match Irgen::default().translate(unit) {
Ok(ir) => ir,
Err(irgen_error) => panic!("{}", irgen_error),
};
let ir = Irgen::default()
.translate(&unit)
.unwrap_or_else(|irgen_error| panic!("{}", irgen_error));
let temp_dir = tempdir().expect("temp dir creation failed");
let temp_file_path = temp_dir.path().join("temp.c");
let mut temp_file = File::create(&temp_file_path).unwrap();
@@ -221,101 +199,51 @@ pub fn test_opt<P1: AsRef<Path>, P2: AsRef<Path>, O: Optimize<ir::TranslationUni
}
}
pub fn test_asmgen(unit: &TranslationUnit, path: &Path) {
pub fn test_asmgen(path: &Path) {
// TODO: delete black list in the future
let exclusion_list = vec![
"examples/c/struct.c",
"examples/c/struct2.c",
"examples/c/temp2.c",
let ignore_list = vec![
"examples/asmgen/array5.ir",
"examples/asmgen/foo3.ir",
"examples/asmgen/minus_constant.ir",
"examples/asmgen/struct.ir",
"examples/asmgen/struct2.ir",
"examples/asmgen/struct3.ir",
"examples/asmgen/temp2.ir",
"examples/asmgen/typecast.ir",
];
if exclusion_list.contains(&path.to_str().expect("`path` must be transformed to `&str`")) {
if ignore_list.contains(&path.to_str().expect("`path` must be transformed to `&str`")) {
println!("skip test");
return;
}
// Check if the file has .c extension
assert_eq!(path.extension(), Some(std::ffi::OsStr::new("c")));
// Test parse
c::Parse::default()
// Check if the file has .ir extension
assert_eq!(path.extension(), Some(std::ffi::OsStr::new("ir")));
let unit = ir::Parse::default()
.translate(&path)
.expect("failed to parse the given program");
.unwrap_or_else(|_| panic!("parse failed {}", path.display()));
let file_path = path.display().to_string();
let bin_path = path
.with_extension("asmgen")
.as_path()
.display()
.to_string();
// Compile c file: If fails, test is vacuously success
if !Command::new("gcc")
.args(&[
"-fsanitize=undefined",
"-fno-sanitize-recover=all",
"-O1",
&file_path,
"-o",
&bin_path,
])
.stderr(Stdio::null())
.status()
.unwrap()
.success()
{
::std::process::exit(SKIP_TEST);
}
// Execute compiled executable
let mut child = Command::new(fs::canonicalize(bin_path.clone()).unwrap())
.stderr(Stdio::piped())
.spawn()
.expect("failed to execute the compiled executable");
Command::new("rm")
.arg(bin_path)
.status()
.expect("failed to remove compiled executable");
let status = some_or!(
child
.wait_timeout_ms(500)
.expect("failed to obtain exit status from child process"),
{
println!("timeout occurs");
child.kill().unwrap();
child.wait().unwrap();
::std::process::exit(SKIP_TEST);
}
);
if child
.stderr
.expect("`stderr` of `child` must be `Some`")
.bytes()
.next()
.is_some()
{
println!("error occurs");
::std::process::exit(SKIP_TEST);
}
let gcc_status = some_or_exit!(status.code(), SKIP_TEST);
let ir = match Irgen::default().translate(unit) {
Ok(ir) => ir,
Err(irgen_error) => panic!("{}", irgen_error),
};
// Execute IR
let args = Vec::new();
let result = ir::interp(&unit, args).unwrap_or_else(|interp_error| panic!("{}", interp_error));
// We only allow main function whose return type is `int`
let (value, width, is_signed) = result.get_int().expect("non-integer value occurs");
assert_eq!(width, 32);
assert!(is_signed);
// Generate RISC-V assembly from IR
let asm = Asmgen::default()
.translate(&ir)
.translate(&unit)
.expect("fail to create riscv assembly code");
let asm_path = path.with_extension("S").as_path().display().to_string();
let mut buffer = File::create(Path::new(&asm_path)).expect("need to success creating file");
write(&asm, &mut buffer).unwrap();
// Link to an RISC-V executable
let bin_path = path.with_extension("asm").as_path().display().to_string();
let bin_path = path
.with_extension("asmgen")
.as_path()
.display()
.to_string();
if !Command::new("riscv64-linux-gnu-gcc-10")
.args(&["-static", &asm_path, "-o", &bin_path])
.stderr(Stdio::null())
@@ -366,8 +294,8 @@ pub fn test_asmgen(unit: &TranslationUnit, path: &Path) {
::std::process::exit(SKIP_TEST);
}
let kecc_status = some_or_exit!(status.code(), SKIP_TEST);
let qemu_status = some_or_exit!(status.code(), SKIP_TEST);
println!("gcc: {}, kecc: {}", gcc_status, kecc_status);
assert_eq!(gcc_status, kecc_status);
println!("kecc interp: {}, qemu: {}", value as i8, qemu_status as i8);
assert_eq!(value as i8, qemu_status as i8);
}