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

@@ -1312,7 +1312,7 @@ impl fmt::Display for Dtype {
}
Self::Function { ret, params } => write!(
f,
"{} ({})",
"[ret:{} params:({})]",
ret,
params
.iter()

View File

@@ -685,6 +685,15 @@ impl Constant {
}
}
#[inline]
pub fn get_global_variable_name(&self) -> Option<String> {
if let Self::GlobalVariable { name, .. } = self {
Some(name.clone())
} else {
None
}
}
#[inline]
fn minus(self) -> Self {
match self {

View File

@@ -32,16 +32,16 @@ peg::parser! {
})
}
/
"fun" __ dtype:dtype() __ var:global_variable() _ "{" _ fun_body:fun_body() _ "}" {
"fun" __ dtype:dtype() __ var:global_variable() _ "(" params:(dtype() ** (_ "," _)) _ ")" _ "{" _ fun_body:fun_body() _ "}" {
Named::new(Some(var), Declaration::Function {
signature: FunctionSignature::new(Dtype::function(Dtype::int(32), Vec::new())),
signature: FunctionSignature::new(Dtype::function(dtype, params)),
definition: Some(fun_body),
})
}
/
"fun" __ dtype:dtype() __ var:global_variable() {
"fun" __ dtype:dtype() __ var:global_variable() _ "(" params:(dtype() ** (_ "," _)) _ ")" {
Named::new(Some(var), Declaration::Function {
signature: FunctionSignature::new(Dtype::function(Dtype::int(32), Vec::new())),
signature: FunctionSignature::new(Dtype::function(dtype, params)),
definition: None,
})
}
@@ -52,9 +52,24 @@ peg::parser! {
"u" n:number() { Dtype::int(n).set_signed(false) }
/
"i" n:number() { Dtype::int(n) }
/
"f" n:number() { Dtype::float(n) }
/
"*" _ "const" _ inner:dtype() { Dtype::pointer(inner).set_const(true) }
/
"*" _ inner:dtype() { Dtype::pointer(inner) }
/ expected!("dtype")
/
"[" _ n:number() __ "x" __ inner:dtype() _ "]" {
Dtype::Array { inner: Box::new(inner), size: n }
}
/
"[ret:" _ ret:dtype() __ "params:(" params:(dtype() ** (_ "," _)) _ ")]" {
Dtype::Function { ret: Box::new(ret), params }
}
/
"const" __ dtype:dtype() { dtype.set_const(true) }
/
expected!("dtype")
rule id() -> String
= n:$(['_' | 'a'..='z' | 'A'..='Z']['_' | 'a'..='z' | 'A'..='Z' | '0'..='9']*) {
@@ -111,6 +126,12 @@ peg::parser! {
}
/ expected!("number")
rule float_number() -> f64
= f:$(['0'..='9']+['.']['0'..='9']+) {
f.parse().unwrap()
}
/ expected!("float_number")
rule bid() -> BlockId
= "b" n:number() {
BlockId(n)
@@ -125,6 +146,17 @@ peg::parser! {
rule instruction() -> (BlockId, usize, Named<Instruction>)
= "%" bid:bid() ":i" number:number() ":" dtype:dtype() name:(":" name:id() { name })? _ "=" _ instruction:instruction_inner() {
// TODO: The dtype of `GetElementPtr` instruction depends on the situation.
// Let's `ptr` has `*[5 x i32]` type, after applying `GetElementPtr` instruction,
// the dtype of the result can be `*i32` or `*[5 x i32]` in the current KECC.
// For this reason, we need to check the dtype of the result to confirm the dtype
// of `GetElementPtr` instruction when parsing IR.
let instruction = if let Instruction::GetElementPtr { ptr, offset, .. } = instruction {
Instruction::GetElementPtr { ptr, offset, dtype: Box::new(dtype) }
} else {
instruction
};
(bid, number, Named::new(name, instruction))
}
/ expected!("instruction")
@@ -143,10 +175,19 @@ peg::parser! {
}
/
"call" __ callee:operand() _ "(" _ args:(operand() ** (_ "," _)) _ ")" {
let dtype_of_callee = callee.dtype();
let function_type = dtype_of_callee
.get_pointer_inner()
.expect("`callee`'s dtype must be function pointer type");
let return_type = function_type
.get_function_inner()
.expect("`callee`'s dtype must be function pointer type")
.0.clone();
Instruction::Call {
callee,
args,
return_type: Dtype::unit(), // TODO
return_type,
}
}
/
@@ -154,10 +195,10 @@ peg::parser! {
Instruction::TypeCast { value, target_dtype }
}
/
"minus" __ operand:operand() {
op:unary_op() __ operand:operand() {
let dtype = operand.dtype();
Instruction::UnaryOp {
op: UnaryOperator::Minus,
op,
operand,
dtype,
}
@@ -173,6 +214,17 @@ peg::parser! {
dtype,
}
}
/
op:shift_op() __ lhs:operand() __ rhs:operand() {
let dtype = lhs.dtype();
assert_eq!(&dtype, &rhs.dtype());
Instruction::BinOp {
op,
lhs,
rhs,
dtype,
}
}
/
"cmp" __ op:comparison_op() __ lhs:operand() __ rhs:operand() {
assert_eq!(lhs.dtype(), rhs.dtype());
@@ -183,6 +235,25 @@ peg::parser! {
dtype: Dtype::BOOL,
}
}
/
op:bitwise_op() __ lhs:operand() __ rhs:operand() {
let dtype = lhs.dtype();
assert_eq!(&dtype, &rhs.dtype());
Instruction::BinOp {
op,
lhs,
rhs,
dtype,
}
}
/
"getelementptr" __ ptr:operand() __ "offset" __ offset:operand() {
Instruction::GetElementPtr{
ptr,
offset,
dtype: Box::new(Dtype::unit()), // TODO
}
}
/
"<instruction>" {
todo!()
@@ -193,6 +264,17 @@ peg::parser! {
"add" { BinaryOperator::Plus }
/
"sub" { BinaryOperator::Minus }
/
"mul" { BinaryOperator::Multiply }
/
"div" { BinaryOperator::Divide }
/
"mod" { BinaryOperator::Modulo }
rule shift_op() -> BinaryOperator =
"shl" { BinaryOperator::ShiftLeft }
/
"shr" { BinaryOperator::ShiftRight }
rule comparison_op() -> BinaryOperator =
"eq" { BinaryOperator::Equals }
@@ -207,12 +289,26 @@ peg::parser! {
/
"ge" { BinaryOperator::GreaterOrEqual }
rule bitwise_op() -> BinaryOperator =
"and" { BinaryOperator::BitwiseAnd }
/
"xor" { BinaryOperator::BitwiseXor }
/
"or" { BinaryOperator::BitwiseOr }
rule unary_op() -> UnaryOperator =
"plus" { UnaryOperator::Plus }
/
"minus" { UnaryOperator::Minus }
/
"negate" { UnaryOperator::Negate }
rule exit() -> BlockExit =
"j" __ arg:jump_arg() {
BlockExit::Jump { arg }
}
/
"br" __ condition:operand() __ arg_then:jump_arg() __ arg_else:jump_arg() {
"br" __ condition:operand() _ "," _ arg_then:jump_arg() _ "," _ arg_else:jump_arg() {
BlockExit::ConditionalJump { condition, arg_then: Box::new(arg_then), arg_else: Box::new(arg_else) }
}
/
@@ -229,6 +325,10 @@ peg::parser! {
}
rule constant() -> Constant =
f:float_number() {
Constant::float(f, Dtype::float(64)) // TODO: the right dtype
}
/
n:number() {
Constant::int(n as _, Dtype::int(128)) // TODO: the right dtype
}
@@ -240,6 +340,13 @@ peg::parser! {
"unit" {
Constant::unit()
}
/
name:global_variable() {
Constant::GlobalVariable {
name,
dtype: Dtype::unit(), // TODO
}
}
/
"<constant>" {
todo!()
@@ -260,13 +367,6 @@ peg::parser! {
}
rule operand() -> Operand =
name:global_variable() {
Operand::Constant(Constant::GlobalVariable {
name,
dtype: Dtype::unit(), // TODO
})
}
/
constant:constant() ":" dtype:dtype() {
let constant = match (&constant, &dtype) {
(Constant::Int { value, .. }, Dtype::Int { width, is_signed, .. }) => {
@@ -276,9 +376,19 @@ peg::parser! {
is_signed: *is_signed,
}
}
(Constant::Float { value, .. }, Dtype::Float { width, .. }) => {
Constant::Float {
value: *value,
width: *width,
}
}
(Constant::Undef { .. }, _) => {
Constant::undef(dtype.clone())
}
(Constant::GlobalVariable { name, .. }, _) => {
let dtype_of_inner = dtype.get_pointer_inner().expect("`dtype` must be pointer type");
Constant::global_variable(name.clone(), dtype_of_inner.clone())
}
_ => constant.clone(),
};
Operand::Constant(constant)
@@ -314,6 +424,7 @@ peg::parser! {
pub enum Error {
IoError(std::io::Error),
ParseError(peg::error::ParseError<peg::str::LineCol>),
ResolveError,
}
#[derive(Default)]

View File

@@ -70,9 +70,16 @@ impl WriteLine for (&String, &Declaration) {
signature,
definition,
} => {
let params = signature
.params
.iter()
.map(|p| p.to_string())
.collect::<Vec<_>>()
.join(", ");
if let Some(definition) = definition.as_ref() {
// print function definition
writeln!(write, "fun {} @{} {{", signature.ret, name)?;
writeln!(write, "fun {} @{} ({}) {{", signature.ret, name, params)?;
// print meta data for function
writeln!(
write,
@@ -104,7 +111,7 @@ impl WriteLine for (&String, &Declaration) {
writeln!(write, "}}")?;
} else {
// print declaration line only
writeln!(write, "fun {} @{}", signature.ret, name)?;
writeln!(write, "fun {} @{} ({})", signature.ret, name, params)?;
writeln!(write)?;
}
}
@@ -173,7 +180,7 @@ impl WriteString for Instruction {
Instruction::Load { ptr } => format!("load {}", ptr.write_string()),
Instruction::Call { callee, args, .. } => format!(
"call {}({})",
callee,
callee.write_string(),
args.iter()
.map(WriteString::write_string)
.collect::<Vec<_>>()
@@ -183,9 +190,11 @@ impl WriteString for Instruction {
value,
target_dtype,
} => format!("typecast {} to {}", value.write_string(), target_dtype),
Instruction::GetElementPtr { ptr, offset, .. } => {
format!("getelementptr {} offset {}", ptr, offset)
}
Instruction::GetElementPtr { ptr, offset, .. } => format!(
"getelementptr {} offset {}",
ptr.write_string(),
offset.write_string()
),
}
}
}

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);
}