mirror of
https://github.com/kmc7468/cs420.git
synced 2025-12-16 15:38:48 +00:00
Add skeleton for arrays in IR
This commit is contained in:
@@ -729,7 +729,10 @@ impl Dtype {
|
||||
}
|
||||
Self::Array { inner, size } => {
|
||||
let inner = inner.deref().clone().resolve_typedefs(typedefs)?;
|
||||
Dtype::array(inner, *size)
|
||||
Dtype::Array {
|
||||
inner: Box::new(inner),
|
||||
size: *size,
|
||||
}
|
||||
}
|
||||
Self::Function { ret, params } => {
|
||||
let ret = ret.deref().clone().resolve_typedefs(typedefs)?;
|
||||
|
||||
@@ -950,6 +950,27 @@ impl<'i> State<'i> {
|
||||
}
|
||||
})?
|
||||
}
|
||||
Instruction::GetElementPtr { ptr, offset, dtype } => {
|
||||
let ptr = self.interp_operand(ptr.clone())?;
|
||||
|
||||
let (value, _, _) = self
|
||||
.interp_operand(offset.clone())?
|
||||
.get_int()
|
||||
.expect("`idx` must be `Value::Int`");
|
||||
|
||||
let (bid, prev_offset, ..) = ptr
|
||||
.get_pointer()
|
||||
.expect("`pointer` must be `Value::Pointer` to access memory");
|
||||
|
||||
let inner_dtype = dtype
|
||||
.get_pointer_inner()
|
||||
.expect("`dtype` must be pointer type");
|
||||
|
||||
let offset = prev_offset + value as isize;
|
||||
assert!(0 <= offset);
|
||||
|
||||
Value::pointer(*bid, offset as isize, inner_dtype.clone())
|
||||
}
|
||||
};
|
||||
|
||||
let register = RegisterId::temp(self.stack_frame.pc.bid, self.stack_frame.pc.iid);
|
||||
|
||||
@@ -210,6 +210,13 @@ pub enum Instruction {
|
||||
value: Operand,
|
||||
target_dtype: Dtype,
|
||||
},
|
||||
/// `GetElementPtr` is inspired from `getelementptr` instruction of LLVM.
|
||||
/// https://llvm.org/docs/LangRef.html#i-getelementptr
|
||||
GetElementPtr {
|
||||
ptr: Operand,
|
||||
offset: Operand,
|
||||
dtype: Box<Dtype>,
|
||||
},
|
||||
}
|
||||
|
||||
impl HasDtype for Instruction {
|
||||
@@ -228,6 +235,7 @@ impl HasDtype for Instruction {
|
||||
.set_const(false),
|
||||
Self::Call { return_type, .. } => return_type.clone(),
|
||||
Self::TypeCast { target_dtype, .. } => target_dtype.clone(),
|
||||
Self::GetElementPtr { dtype, .. } => dtype.deref().clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,6 +174,9 @@ impl WriteString for Instruction {
|
||||
value,
|
||||
target_dtype,
|
||||
} => format!("typecast {} to {}", value.write_string(), target_dtype),
|
||||
Instruction::GetElementPtr { ptr, offset, .. } => {
|
||||
format!("getelementptr {} offset {}", ptr, offset)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user