codegen: store to list element

This commit is contained in:
pca006132 2021-08-19 17:14:38 +08:00
parent 0e2da0d180
commit e47d063efc
2 changed files with 21 additions and 5 deletions

View File

@ -14,7 +14,7 @@ use inkwell::{
use itertools::{chain, izip, zip, Itertools}; use itertools::{chain, izip, zip, Itertools};
use rustpython_parser::ast::{self, Boolop, Constant, Expr, ExprKind, Operator}; use rustpython_parser::ast::{self, Boolop, Constant, Expr, ExprKind, Operator};
fn assert_int_val<'ctx>(val: BasicValueEnum<'ctx>) -> IntValue<'ctx> { pub fn assert_int_val<'ctx>(val: BasicValueEnum<'ctx>) -> IntValue<'ctx> {
if let BasicValueEnum::IntValue(v) = val { if let BasicValueEnum::IntValue(v) = val {
v v
} else { } else {
@ -22,7 +22,7 @@ fn assert_int_val<'ctx>(val: BasicValueEnum<'ctx>) -> IntValue<'ctx> {
} }
} }
fn assert_pointer_val<'ctx>(val: BasicValueEnum<'ctx>) -> PointerValue<'ctx> { pub fn assert_pointer_val<'ctx>(val: BasicValueEnum<'ctx>) -> PointerValue<'ctx> {
if let BasicValueEnum::PointerValue(v) = val { if let BasicValueEnum::PointerValue(v) = val {
v v
} else { } else {
@ -556,7 +556,6 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
let arr_ptr = let arr_ptr =
assert_pointer_val(self.builder.build_load(ptr_to_arr, "loadptr")); assert_pointer_val(self.builder.build_load(ptr_to_arr, "loadptr"));
let ptr = self.builder.build_gep(arr_ptr, &[index], "loadarrgep"); let ptr = self.builder.build_gep(arr_ptr, &[index], "loadarrgep");
println!("building element pointer");
self.builder.build_load(ptr, "loadarr") self.builder.build_load(ptr, "loadarr")
} }
} }

View File

@ -1,4 +1,7 @@
use super::CodeGenContext; use super::{
expr::{assert_int_val, assert_pointer_val},
CodeGenContext,
};
use crate::typecheck::typedef::Type; use crate::typecheck::typedef::Type;
use inkwell::values::{BasicValue, BasicValueEnum, PointerValue}; use inkwell::values::{BasicValue, BasicValueEnum, PointerValue};
use rustpython_parser::ast::{Expr, ExprKind, Stmt, StmtKind}; use rustpython_parser::ast::{Expr, ExprKind, Stmt, StmtKind};
@ -45,7 +48,21 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
) )
} }
} }
ExprKind::Subscript { .. } => unimplemented!(), ExprKind::Subscript { value, slice, .. } => {
let i32_type = self.ctx.i32_type();
let v = assert_pointer_val(self.gen_expr(value).unwrap());
let index = assert_int_val(self.gen_expr(slice).unwrap());
unsafe {
let ptr_to_arr = self.builder.build_in_bounds_gep(
v,
&[i32_type.const_zero(), i32_type.const_int(1, false)],
"ptr_to_arr",
);
let arr_ptr =
assert_pointer_val(self.builder.build_load(ptr_to_arr, "loadptr"));
self.builder.build_gep(arr_ptr, &[index], "loadarrgep")
}
}
_ => unreachable!(), _ => unreachable!(),
} }
} }