mirror of
https://github.com/kmc7468/cs420.git
synced 2025-12-14 22:38:46 +00:00
HW2 (3)
examples/c/array.c ~ array4.c까지 통과하는 상태
This commit is contained in:
@@ -1340,6 +1340,18 @@ impl IrgenFunc<'_> {
|
||||
|
||||
context.insert_instruction(ir::Instruction::Load { ptr: tmp })
|
||||
}
|
||||
BinaryOperator::Index => {
|
||||
let ptr = self.translate_index_op(lhs, rhs, context)?;
|
||||
if let Some(inner) = ptr.dtype().get_pointer_inner().unwrap().get_array_inner() {
|
||||
context.insert_instruction(ir::Instruction::GetElementPtr {
|
||||
ptr,
|
||||
offset: ir::Operand::constant(ir::Constant::int(0, ir::Dtype::int(32))),
|
||||
dtype: ir::Dtype::pointer(inner.clone()),
|
||||
})
|
||||
} else {
|
||||
context.insert_instruction(ir::Instruction::Load { ptr })
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let lhs = self.translate_expr_rvalue(lhs, context)?;
|
||||
let rhs = self.translate_expr_rvalue(rhs, context)?;
|
||||
@@ -1410,12 +1422,25 @@ impl IrgenFunc<'_> {
|
||||
UnaryOperator::PostIncrement => {
|
||||
let lhs = self.translate_expr_rvalue(operand, context)?;
|
||||
let dtype = lhs.dtype();
|
||||
let rhs = context.insert_instruction(ir::Instruction::BinOp {
|
||||
op: BinaryOperator::Plus,
|
||||
lhs: lhs.clone(),
|
||||
rhs: ir::Operand::constant(ir::Constant::int(1, dtype.clone())),
|
||||
dtype: dtype.clone(),
|
||||
})?;
|
||||
let rhs = if let Some(inner) = dtype.get_pointer_inner() {
|
||||
let (size, _) = ir::Dtype::size_align_of(inner, self.structs)
|
||||
.map_err(|e| IrgenErrorMessage::InvalidDtype { dtype_error: e })?;
|
||||
context.insert_instruction(ir::Instruction::GetElementPtr {
|
||||
ptr: lhs.clone(),
|
||||
offset: ir::Operand::constant(ir::Constant::int(
|
||||
size.try_into().unwrap(),
|
||||
ir::Dtype::int(32),
|
||||
)),
|
||||
dtype: dtype.clone(),
|
||||
})?
|
||||
} else {
|
||||
context.insert_instruction(ir::Instruction::BinOp {
|
||||
op: BinaryOperator::Plus,
|
||||
lhs: lhs.clone(),
|
||||
rhs: ir::Operand::constant(ir::Constant::int(1, dtype.clone())),
|
||||
dtype: dtype.clone(),
|
||||
})?
|
||||
};
|
||||
let lhs_lvalue = self.translate_expr_lvalue(operand, context)?;
|
||||
let _unused = context.insert_instruction(ir::Instruction::Store {
|
||||
ptr: lhs_lvalue,
|
||||
@@ -1488,7 +1513,18 @@ impl IrgenFunc<'_> {
|
||||
array_inner: &ir::Dtype,
|
||||
context: &mut Context,
|
||||
) -> Result<ir::Operand, IrgenErrorMessage> {
|
||||
todo!("array to pointer")
|
||||
context.insert_instruction(ir::Instruction::GetElementPtr {
|
||||
ptr: ptr.clone(),
|
||||
offset: ir::Operand::constant(ir::Constant::int(0, ir::Dtype::int(32))),
|
||||
dtype: ir::Dtype::pointer(
|
||||
ptr.dtype()
|
||||
.get_pointer_inner()
|
||||
.unwrap()
|
||||
.get_array_inner()
|
||||
.cloned()
|
||||
.unwrap(),
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
fn translate_condition(
|
||||
@@ -1739,7 +1775,36 @@ impl IrgenFunc<'_> {
|
||||
rhs: &Expression,
|
||||
context: &mut Context,
|
||||
) -> Result<ir::Operand, IrgenErrorMessage> {
|
||||
todo!("index op")
|
||||
let lhs: ir::Operand = self.translate_expr_rvalue(lhs, context)?;
|
||||
let lhs_dtype = lhs.dtype();
|
||||
let (dtype, inner_dtype) = if let Some(inner) = lhs_dtype.get_array_inner() {
|
||||
(ir::Dtype::pointer(inner.clone()), inner.clone())
|
||||
} else if let Some(inner) = lhs_dtype.get_pointer_inner() {
|
||||
// println!("hi~ {}", lhs_inner_dtype);
|
||||
(lhs_dtype.clone(), inner.clone())
|
||||
} else {
|
||||
panic!("WTF")
|
||||
};
|
||||
let rhs = self.translate_expr_rvalue(rhs, context)?;
|
||||
let rhs_dtype = rhs.dtype();
|
||||
let (inner_size, _) = inner_dtype
|
||||
.size_align_of(self.structs)
|
||||
.map_err(|e| IrgenErrorMessage::InvalidDtype { dtype_error: e })?;
|
||||
let offset = context.insert_instruction(ir::Instruction::BinOp {
|
||||
op: BinaryOperator::Multiply,
|
||||
lhs: rhs,
|
||||
rhs: ir::Operand::constant(ir::Constant::int(
|
||||
inner_size.try_into().unwrap(),
|
||||
rhs_dtype.clone(),
|
||||
)),
|
||||
dtype: rhs_dtype.clone(),
|
||||
})?;
|
||||
// println!("asdfasdf {}", dtype);
|
||||
context.insert_instruction(ir::Instruction::GetElementPtr {
|
||||
ptr: lhs.clone(),
|
||||
offset,
|
||||
dtype,
|
||||
})
|
||||
}
|
||||
|
||||
fn translate_func_call(
|
||||
|
||||
Reference in New Issue
Block a user