mirror of
https://github.com/kmc7468/cs420.git
synced 2025-12-16 15:38:48 +00:00
Fix hw1 fuzzer again
This commit is contained in:
@@ -44,6 +44,10 @@ pub enum Dtype {
|
||||
inner: Box<Dtype>,
|
||||
is_const: bool,
|
||||
},
|
||||
Array {
|
||||
inner: Box<Dtype>,
|
||||
size: usize,
|
||||
},
|
||||
Function {
|
||||
ret: Box<Dtype>,
|
||||
params: Vec<Dtype>,
|
||||
@@ -342,6 +346,30 @@ impl Dtype {
|
||||
}
|
||||
}
|
||||
|
||||
// Suppose the C declaration is `int *a[2][3]`. Then `a`'s `ir::Dtype` should be `[2 x [3 x int*]]`.
|
||||
// But in the AST, it is parsed as `Array(3, Array(2, Pointer(int)))`, reversing the order of `2` and `3`.
|
||||
// In the recursive translation of declaration into Dtype, we need to insert `3` inside `[2 * int*]`.
|
||||
pub fn array(base_dtype: Dtype, size: usize) -> Self {
|
||||
match base_dtype {
|
||||
Self::Array {
|
||||
inner,
|
||||
size: old_size,
|
||||
} => {
|
||||
let inner = inner.deref().clone();
|
||||
let inner = Self::array(inner, size);
|
||||
Self::Array {
|
||||
inner: Box::new(inner),
|
||||
size: old_size,
|
||||
}
|
||||
}
|
||||
Self::Function { .. } => panic!("array size cannot be applied to function type"),
|
||||
inner => Self::Array {
|
||||
inner: Box::new(inner),
|
||||
size,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn function(ret: Dtype, params: Vec<Dtype>) -> Self {
|
||||
Self::Function {
|
||||
@@ -411,9 +439,8 @@ impl Dtype {
|
||||
Self::Int { is_const, .. } => *is_const,
|
||||
Self::Float { is_const, .. } => *is_const,
|
||||
Self::Pointer { is_const, .. } => *is_const,
|
||||
Self::Function { .. } => {
|
||||
panic!("there should be no case that check whether `Function` is `const`")
|
||||
}
|
||||
Self::Array { .. } => true,
|
||||
Self::Function { .. } => true,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -429,36 +456,36 @@ impl Dtype {
|
||||
},
|
||||
Self::Float { width, .. } => Self::Float { width, is_const },
|
||||
Self::Pointer { inner, .. } => Self::Pointer { inner, is_const },
|
||||
Self::Function { .. } => panic!("`const` cannot be applied to `Dtype::Function`"),
|
||||
Self::Array { .. } => self,
|
||||
Self::Function { .. } => self,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return byte size of `Dtype`
|
||||
pub fn size_of(&self) -> Result<usize, DtypeError> {
|
||||
// TODO: consider complex type like array, structure in the future
|
||||
pub fn size_align_of(&self) -> Result<(usize, usize), DtypeError> {
|
||||
match self {
|
||||
Self::Unit { .. } => Ok(0),
|
||||
Self::Int { width, .. } => Ok(*width / Self::WIDTH_OF_BYTE),
|
||||
Self::Float { width, .. } => Ok(*width / Self::WIDTH_OF_BYTE),
|
||||
Self::Pointer { .. } => Ok(Self::WIDTH_OF_POINTER / Self::WIDTH_OF_BYTE),
|
||||
Self::Function { .. } => Err(DtypeError::Misc {
|
||||
message: "`sizeof` cannot be used with function types".to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
Self::Unit { .. } => Ok((0, 1)),
|
||||
Self::Int { width, .. } | Self::Float { width, .. } => {
|
||||
let align_of = *width / Self::WIDTH_OF_BYTE;
|
||||
let size_of = align_of;
|
||||
|
||||
/// Return alignment requirements of `Dtype`
|
||||
pub fn align_of(&self) -> Result<usize, DtypeError> {
|
||||
// TODO: consider complex type like array, structure in the future
|
||||
// TODO: when considering complex type like a structure,
|
||||
// the calculation method should be different from `Dtype::size_of`.
|
||||
match self {
|
||||
Self::Unit { .. } => Ok(0),
|
||||
Self::Int { width, .. } => Ok(*width / Self::WIDTH_OF_BYTE),
|
||||
Self::Float { width, .. } => Ok(*width / Self::WIDTH_OF_BYTE),
|
||||
Self::Pointer { .. } => Ok(Self::WIDTH_OF_POINTER / Self::WIDTH_OF_BYTE),
|
||||
Ok((size_of, align_of))
|
||||
}
|
||||
Self::Pointer { .. } => {
|
||||
let align_of = Self::WIDTH_OF_POINTER / Self::WIDTH_OF_BYTE;
|
||||
let size_of = align_of;
|
||||
|
||||
Ok((size_of, align_of))
|
||||
}
|
||||
Self::Array { inner, size, .. } => {
|
||||
let (size_of_inner, align_of_inner) = inner.size_align_of()?;
|
||||
|
||||
Ok((
|
||||
size * std::cmp::max(size_of_inner, align_of_inner),
|
||||
align_of_inner,
|
||||
))
|
||||
}
|
||||
Self::Function { .. } => Err(DtypeError::Misc {
|
||||
message: "`alignof` cannot be used with function types".to_string(),
|
||||
message: "`size_align_of` cannot be used with function types".to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -590,6 +617,7 @@ impl fmt::Display for Dtype {
|
||||
Self::Pointer { inner, is_const } => {
|
||||
write!(f, "{}* {}", inner, if *is_const { "const" } else { "" })
|
||||
}
|
||||
Self::Array { inner, size, .. } => write!(f, "[{} x {}]", size, inner,),
|
||||
Self::Function { ret, params } => write!(
|
||||
f,
|
||||
"{} ({})",
|
||||
|
||||
@@ -11,6 +11,9 @@ use crate::*;
|
||||
// TODO: the variants of Value will be added in the future
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum Value {
|
||||
Undef {
|
||||
dtype: Dtype,
|
||||
},
|
||||
Unit,
|
||||
Int {
|
||||
value: u128,
|
||||
@@ -97,7 +100,8 @@ impl Value {
|
||||
} => Self::int(u128::default(), *width, *is_signed),
|
||||
ir::Dtype::Float { width, .. } => Self::float(f64::default(), *width),
|
||||
ir::Dtype::Pointer { .. } => Self::nullptr(),
|
||||
ir::Dtype::Function { .. } => panic!("function types do not have a default value"),
|
||||
ir::Dtype::Array { .. } => panic!("array type does not have a default value"),
|
||||
ir::Dtype::Function { .. } => panic!("function type does not have a default value"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -225,6 +229,8 @@ mod calculator {
|
||||
rhs: Value,
|
||||
) -> Result<Value, ()> {
|
||||
match (op, lhs, rhs) {
|
||||
(_, Value::Undef { .. }, _) => Err(()),
|
||||
(_, _, Value::Undef { .. }) => Err(()),
|
||||
(
|
||||
op,
|
||||
Value::Int {
|
||||
@@ -273,6 +279,7 @@ mod calculator {
|
||||
operand: Value,
|
||||
) -> Result<Value, ()> {
|
||||
match (op, operand) {
|
||||
(_, Value::Undef { .. }) => Err(()),
|
||||
(
|
||||
ast::UnaryOperator::Plus,
|
||||
Value::Int {
|
||||
@@ -312,6 +319,7 @@ mod calculator {
|
||||
|
||||
pub fn calculate_typecast(value: Value, dtype: crate::ir::Dtype) -> Result<Value, ()> {
|
||||
match (value, dtype) {
|
||||
(Value::Undef { .. }, _) => Err(()),
|
||||
// TODO: distinguish zero/signed extension in the future
|
||||
// TODO: consider truncate in the future
|
||||
(
|
||||
@@ -336,14 +344,7 @@ struct Memory {
|
||||
|
||||
impl Memory {
|
||||
fn alloc(&mut self, dtype: &Dtype) -> Result<usize, InterpreterError> {
|
||||
let memory_block = match dtype {
|
||||
ir::Dtype::Unit { .. }
|
||||
| ir::Dtype::Int { .. }
|
||||
| ir::Dtype::Float { .. }
|
||||
| ir::Dtype::Pointer { .. } => vec![Value::default_from_dtype(dtype)],
|
||||
ir::Dtype::Function { .. } => vec![],
|
||||
};
|
||||
|
||||
let memory_block = Self::block_from_dtype(dtype);
|
||||
self.inner.push(memory_block);
|
||||
|
||||
Ok(self.inner.len() - 1)
|
||||
@@ -356,6 +357,25 @@ impl Memory {
|
||||
fn store(&mut self, bid: usize, offset: usize, value: Value) {
|
||||
self.inner[bid][offset] = value;
|
||||
}
|
||||
|
||||
fn block_from_dtype(dtype: &Dtype) -> Vec<Value> {
|
||||
match dtype {
|
||||
ir::Dtype::Unit { .. } => vec![],
|
||||
ir::Dtype::Int { .. } | ir::Dtype::Float { .. } | ir::Dtype::Pointer { .. } => {
|
||||
vec![Value::Undef {
|
||||
dtype: dtype.clone(),
|
||||
}]
|
||||
}
|
||||
ir::Dtype::Array { inner, size, .. } => {
|
||||
let sub_vec = Self::block_from_dtype(inner.deref());
|
||||
(0..*size).fold(vec![], |mut result, _| {
|
||||
result.append(&mut sub_vec.clone());
|
||||
result
|
||||
})
|
||||
}
|
||||
ir::Dtype::Function { .. } => vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: allocation fields will be added in the future
|
||||
@@ -414,16 +434,20 @@ impl<'i> State<'i> {
|
||||
|
||||
// Initialize allocated memory space
|
||||
match decl {
|
||||
Declaration::Variable { dtype, initializer } => {
|
||||
if dtype.get_function_inner().is_some() {
|
||||
panic!("function variable does not exist")
|
||||
}
|
||||
Declaration::Variable { dtype, initializer } => match &dtype {
|
||||
ir::Dtype::Unit { .. } => (),
|
||||
ir::Dtype::Int { .. } | ir::Dtype::Float { .. } | ir::Dtype::Pointer { .. } => {
|
||||
let value = if let Some(constant) = initializer {
|
||||
self.interp_constant(constant.clone())
|
||||
} else {
|
||||
Value::default_from_dtype(&dtype)
|
||||
};
|
||||
|
||||
if let Some(constant) = initializer {
|
||||
let value = self.interp_constant(constant.clone());
|
||||
self.memory.store(bid, 0, value);
|
||||
}
|
||||
}
|
||||
ir::Dtype::Array { .. } => todo!("Initializer::List is needed"),
|
||||
ir::Dtype::Function { .. } => panic!("function variable does not exist"),
|
||||
},
|
||||
// If functin declaration, skip initialization
|
||||
Declaration::Function { .. } => (),
|
||||
}
|
||||
@@ -578,6 +602,7 @@ impl<'i> State<'i> {
|
||||
|
||||
fn interp_instruction(&mut self, instruction: &Instruction) -> Result<(), InterpreterError> {
|
||||
let result = match instruction {
|
||||
Instruction::Nop => Value::unit(),
|
||||
Instruction::BinOp { op, lhs, rhs, .. } => {
|
||||
let lhs = self.interp_operand(lhs.clone())?;
|
||||
let rhs = self.interp_operand(rhs.clone())?;
|
||||
@@ -685,6 +710,7 @@ impl<'i> State<'i> {
|
||||
|
||||
fn interp_constant(&self, value: Constant) -> Value {
|
||||
match value {
|
||||
Constant::Undef { dtype } => Value::Undef { dtype },
|
||||
Constant::Unit => Value::Unit,
|
||||
Constant::Int {
|
||||
value,
|
||||
|
||||
@@ -49,12 +49,13 @@ impl TryFrom<Dtype> for Declaration {
|
||||
Dtype::Unit { .. } => Err(DtypeError::Misc {
|
||||
message: "A variable of type `void` cannot be declared".to_string(),
|
||||
}),
|
||||
Dtype::Int { .. } | Dtype::Float { .. } | Dtype::Pointer { .. } => {
|
||||
Ok(Declaration::Variable {
|
||||
dtype,
|
||||
initializer: None,
|
||||
})
|
||||
}
|
||||
Dtype::Int { .. }
|
||||
| Dtype::Float { .. }
|
||||
| Dtype::Pointer { .. }
|
||||
| Dtype::Array { .. } => Ok(Declaration::Variable {
|
||||
dtype,
|
||||
initializer: None,
|
||||
}),
|
||||
Dtype::Function { .. } => Ok(Declaration::Function {
|
||||
signature: FunctionSignature::new(dtype),
|
||||
definition: None,
|
||||
@@ -257,6 +258,9 @@ impl Hash for RegisterId {
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum Constant {
|
||||
Undef {
|
||||
dtype: Dtype,
|
||||
},
|
||||
Unit,
|
||||
Int {
|
||||
value: u128,
|
||||
@@ -365,8 +369,14 @@ impl Constant {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn undef(dtype: Dtype) -> Self {
|
||||
Self::Undef { dtype }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn unit() -> Self {
|
||||
Constant::Unit
|
||||
Self::Unit
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -374,7 +384,7 @@ impl Constant {
|
||||
let width = dtype.get_int_width().expect("`dtype` must be `Dtype::Int`");
|
||||
let is_signed = dtype.is_int_signed();
|
||||
|
||||
Constant::Int {
|
||||
Self::Int {
|
||||
value,
|
||||
width,
|
||||
is_signed,
|
||||
@@ -387,18 +397,27 @@ impl Constant {
|
||||
.get_float_width()
|
||||
.expect("`dtype` must be `Dtype::Float`");
|
||||
|
||||
Constant::Float { value, width }
|
||||
Self::Float { value, width }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn global_variable(name: String, dtype: Dtype) -> Self {
|
||||
Self::GlobalVariable { name, dtype }
|
||||
}
|
||||
|
||||
pub fn is_undef(&self) -> bool {
|
||||
if let Self::Undef { .. } = self {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Constant {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Undef { .. } => write!(f, "undef"),
|
||||
Self::Unit => write!(f, "unit"),
|
||||
Self::Int { value, .. } => write!(f, "{}", value),
|
||||
Self::Float { value, .. } => write!(f, "{}", value),
|
||||
@@ -410,6 +429,7 @@ impl fmt::Display for Constant {
|
||||
impl HasDtype for Constant {
|
||||
fn dtype(&self) -> Dtype {
|
||||
match self {
|
||||
Self::Undef { dtype } => dtype.clone(),
|
||||
Self::Unit => Dtype::unit(),
|
||||
Self::Int {
|
||||
width, is_signed, ..
|
||||
@@ -450,6 +470,14 @@ impl Operand {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_register_mut(&mut self) -> Option<(&mut RegisterId, &mut Dtype)> {
|
||||
if let Self::Register { rid, dtype } = self {
|
||||
Some((rid, dtype))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Operand {
|
||||
@@ -471,8 +499,10 @@ impl HasDtype for Operand {
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
pub enum Instruction {
|
||||
// TODO: the variants of Instruction will be added in the future
|
||||
Nop,
|
||||
BinOp {
|
||||
op: ast::BinaryOperator,
|
||||
lhs: Operand,
|
||||
@@ -505,6 +535,7 @@ pub enum Instruction {
|
||||
impl HasDtype for Instruction {
|
||||
fn dtype(&self) -> Dtype {
|
||||
match self {
|
||||
Self::Nop => Dtype::unit(),
|
||||
Self::BinOp { dtype, .. } => dtype.clone(),
|
||||
Self::UnaryOp { dtype, .. } => dtype.clone(),
|
||||
Self::Store { .. } => Dtype::unit(),
|
||||
@@ -521,6 +552,16 @@ impl HasDtype for Instruction {
|
||||
}
|
||||
}
|
||||
|
||||
impl Instruction {
|
||||
pub fn is_pure(&self) -> bool {
|
||||
match self {
|
||||
Self::Store { .. } => false,
|
||||
Self::Call { .. } => false,
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
|
||||
pub struct BlockId(pub usize);
|
||||
|
||||
|
||||
@@ -120,6 +120,7 @@ impl WriteLine for (&BlockId, &Block) {
|
||||
impl WriteString for Instruction {
|
||||
fn write_string(&self) -> String {
|
||||
match self {
|
||||
Instruction::Nop => "nop".into(),
|
||||
Instruction::BinOp { op, lhs, rhs, .. } => format!(
|
||||
"{} {} {}",
|
||||
op.write_operation(),
|
||||
|
||||
Reference in New Issue
Block a user