From 95c43b1d445d09a9f74e99f00d2a0c6627aff112 Mon Sep 17 00:00:00 2001 From: static Date: Tue, 8 Apr 2025 01:02:45 +0000 Subject: [PATCH] HW2 (3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit examples/c/array.c ~ array4.c까지 통과하는 상태 --- src/irgen/mod.rs | 81 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 8 deletions(-) diff --git a/src/irgen/mod.rs b/src/irgen/mod.rs index 5df0adb..c36b32d 100644 --- a/src/irgen/mod.rs +++ b/src/irgen/mod.rs @@ -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 { - 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 { - 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(