Update grader

This commit is contained in:
Chunmyong Park
2020-07-02 06:54:19 +00:00
parent 114f38cbb6
commit d64e9f622e
36 changed files with 314 additions and 149 deletions

View File

@@ -1,3 +1,5 @@
use lang_c::*;
use rand::Rng;
use std::fs::{self, File};
use std::io::{stderr, Read, Write};
use std::path::Path;
@@ -7,6 +9,67 @@ use wait_timeout::ChildExt;
use crate::*;
const NONCE_DTYPE: &str = "int";
const NONCE_NAME: &str = "nonce";
fn modify_c(path: &Path, rand_num: i32) -> String {
let mut src = File::open(path).expect("`path` must exist");
let mut data = String::new();
src.read_to_string(&mut data)
.expect("`src` must be converted to string");
drop(src);
let from = format!("{} {}", NONCE_DTYPE, NONCE_NAME);
let to = format!("{} = {}", from, rand_num);
data.replace(&from, &to)
}
fn ast_initializer(number: i32) -> ast::Initializer {
let expr = ast::Expression::Constant(Box::new(span::Node::new(
ast::Constant::Integer(ast::Integer {
base: ast::IntegerBase::Decimal,
number: Box::from(&number.to_string() as &str),
suffix: ast::IntegerSuffix {
size: ast::IntegerSize::Int,
unsigned: false,
imaginary: false,
},
}),
span::Span::none(),
)));
ast::Initializer::Expression(Box::new(span::Node::new(expr, span::Span::none())))
}
fn modify_ir(unit: &mut ir::TranslationUnit, rand_num: i32) {
for (name, decl) in &mut unit.decls {
if name == NONCE_NAME {
let (dtype, initializer) = decl.get_variable().expect("`decl` must be variable");
assert!(initializer.is_none());
let initializer = ast_initializer(rand_num);
let new_decl = ir::Declaration::Variable {
dtype: dtype.clone(),
initializer: Some(initializer),
};
*decl = new_decl;
}
}
}
fn modify_asm(unit: &mut asm::Asm, rand_num: i32) {
for variable in &mut unit.unit.variables {
let body = &mut variable.body;
if body.label == asm::Label(NONCE_NAME.to_string()) {
let directive =
asm::Directive::try_from_data_size(asm::DataSize::Word, rand_num as u64);
body.directives = vec![directive];
}
}
}
// Rust sets an exit code of 101 when the process panicked.
// So, we decide KECC sets an exit code of 102 after 101 when the test skipped.
pub const SKIP_TEST: i32 = 102;
@@ -39,13 +102,27 @@ pub fn test_irgen(path: &Path) {
.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 mut ir = Irgen::default()
.translate(&unit)
.unwrap_or_else(|irgen_error| panic!("{}", irgen_error));
let file_path = path.display().to_string();
let bin_path = path.with_extension("irgen").as_path().display().to_string();
// Apply random value to global variable `dyn`
let rand_num = rand::thread_rng().gen();
let new_c = modify_c(path, rand_num);
modify_ir(&mut ir, rand_num);
// compile recolved c example
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();
temp_file.write_all(new_c.as_bytes()).unwrap();
let file_path = temp_file_path.display().to_string();
let bin_path = temp_file_path
.with_extension("irgen")
.as_path()
.display()
.to_string();
// Compile c file: If fails, test is vacuously success
if !Command::new("gcc")
@@ -100,10 +177,10 @@ pub fn test_irgen(path: &Path) {
}
let status = some_or_exit!(status.code(), SKIP_TEST);
drop(temp_file);
temp_dir.close().expect("temp dir deletion failed");
let ir = Irgen::default()
.translate(&unit)
.unwrap_or_else(|irgen_error| panic!("{}", irgen_error));
// Interpret resolved ir
let args = Vec::new();
let result = ir::interp(&ir, args).unwrap_or_else(|interp_error| panic!("{}", interp_error));
// We only allow main function whose return type is `int`
@@ -217,22 +294,28 @@ pub fn test_opt<P1: AsRef<Path>, P2: AsRef<Path>, O: Optimize<ir::TranslationUni
pub fn test_asmgen(path: &Path) {
// Check if the file has .ir extension
assert_eq!(path.extension(), Some(std::ffi::OsStr::new("ir")));
let unit = ir::Parse::default()
let mut ir = ir::Parse::default()
.translate(&path)
.unwrap_or_else(|_| panic!("parse failed {}", path.display()));
// Generate RISC-V assembly from IR
let mut asm = Asmgen::default()
.translate(&ir)
.expect("fail to create riscv assembly code");
// Resolve examples
let rand_num = rand::thread_rng().gen();
modify_ir(&mut ir, rand_num);
modify_asm(&mut asm, rand_num);
// Execute IR
let args = Vec::new();
let result = ir::interp(&unit, args).unwrap_or_else(|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);
assert!(is_signed);
// Generate RISC-V assembly from IR
let asm = Asmgen::default()
.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();