Compare commits
3 Commits
20aa094b1f
...
8bef0ab8f2
Author | SHA1 | Date |
---|---|---|
David Mak | 8bef0ab8f2 | |
David Mak | 806f65815f | |
David Mak | 2e6a64a1de |
|
@ -529,7 +529,7 @@ pub fn attributes_writeback<'ctx, 'a>(
|
|||
let index = ctx.get_attr_index(ty, *name);
|
||||
values.push((*field_ty, ctx.build_gep_and_load(
|
||||
obj.into_pointer_value(),
|
||||
&[zero, int32.const_int(index as u64, false)])));
|
||||
&[zero, int32.const_int(index as u64, false)], None)));
|
||||
}
|
||||
}
|
||||
if !attributes.is_empty() {
|
||||
|
|
|
@ -62,8 +62,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
|||
&mut self,
|
||||
ptr: PointerValue<'ctx>,
|
||||
index: &[IntValue<'ctx>],
|
||||
name: Option<&str>,
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
unsafe { self.builder.build_load(self.builder.build_gep(ptr, index, "gep"), "load") }
|
||||
unsafe { self.builder.build_load(self.builder.build_gep(ptr, index, ""), name.unwrap_or_default()) }
|
||||
}
|
||||
|
||||
fn get_subst_key(
|
||||
|
@ -753,43 +754,66 @@ pub fn destructure_range<'ctx, 'a>(
|
|||
) -> (IntValue<'ctx>, IntValue<'ctx>, IntValue<'ctx>) {
|
||||
let int32 = ctx.ctx.i32_type();
|
||||
let start = ctx
|
||||
.build_gep_and_load(range, &[int32.const_zero(), int32.const_int(0, false)])
|
||||
.build_gep_and_load(range, &[int32.const_zero(), int32.const_int(0, false)], Some("range.start"))
|
||||
.into_int_value();
|
||||
let end = ctx
|
||||
.build_gep_and_load(range, &[int32.const_zero(), int32.const_int(1, false)])
|
||||
.build_gep_and_load(range, &[int32.const_zero(), int32.const_int(1, false)], Some("range.stop"))
|
||||
.into_int_value();
|
||||
let step = ctx
|
||||
.build_gep_and_load(range, &[int32.const_zero(), int32.const_int(2, false)])
|
||||
.build_gep_and_load(range, &[int32.const_zero(), int32.const_int(2, false)], Some("range.step"))
|
||||
.into_int_value();
|
||||
(start, end, step)
|
||||
}
|
||||
|
||||
/// Allocates a List structure with the given [type][ty] and [length]. The name of the resulting
|
||||
/// LLVM value is `{name}.addr`, or `list.addr` if [name] is not specified.
|
||||
///
|
||||
/// Returns an instance of [PointerValue] pointing to the List structure. The List structure is
|
||||
/// defined as `type { ty*, size_t }` in LLVM, where the first element stores the pointer to the
|
||||
/// data, and the second element stores the size of the List.
|
||||
pub fn allocate_list<'ctx, 'a, G: CodeGenerator>(
|
||||
generator: &mut G,
|
||||
ctx: &mut CodeGenContext<'ctx, 'a>,
|
||||
ty: BasicTypeEnum<'ctx>,
|
||||
length: IntValue<'ctx>,
|
||||
name: Option<&str>,
|
||||
) -> PointerValue<'ctx> {
|
||||
let arr_ptr = ctx.builder.build_array_alloca(ty, length, "tmparr");
|
||||
let size_t = generator.get_size_type(ctx.ctx);
|
||||
let i32_t = ctx.ctx.i32_type();
|
||||
let arr_ty =
|
||||
ctx.ctx.struct_type(&[ty.ptr_type(AddressSpace::default()).into(), size_t.into()], false);
|
||||
// List structure; type { ty*, size_t }
|
||||
let arr_ty = ctx.ctx
|
||||
.struct_type(&[ty.ptr_type(AddressSpace::default()).into(), size_t.into()], false);
|
||||
let zero = ctx.ctx.i32_type().const_zero();
|
||||
let arr_str_ptr = ctx.builder.build_alloca(arr_ty, "tmparrstr");
|
||||
|
||||
let arr_str_ptr = ctx.builder.build_alloca(
|
||||
arr_ty, format!("{}.addr", name.unwrap_or("list")).as_str()
|
||||
);
|
||||
|
||||
unsafe {
|
||||
// Pointer to the `length` element of the list structure
|
||||
let len_ptr = ctx.builder.build_in_bounds_gep(
|
||||
arr_str_ptr,
|
||||
&[zero, i32_t.const_int(1, false)],
|
||||
"len_ptr",
|
||||
""
|
||||
);
|
||||
let length = ctx.builder.build_int_z_extend(
|
||||
length,
|
||||
size_t,
|
||||
""
|
||||
);
|
||||
let length = ctx.builder.build_int_z_extend(length, size_t, "zext");
|
||||
ctx.builder.build_store(len_ptr, length);
|
||||
let ptr_to_arr =
|
||||
ctx.builder.build_in_bounds_gep(arr_str_ptr, &[zero, i32_t.const_zero()], "ptr_to_arr");
|
||||
|
||||
// Pointer to the `data` element of the list structure
|
||||
let arr_ptr = ctx.builder.build_array_alloca(ty, length, "");
|
||||
let ptr_to_arr = ctx.builder.build_in_bounds_gep(
|
||||
arr_str_ptr,
|
||||
&[zero, i32_t.const_zero()],
|
||||
""
|
||||
);
|
||||
ctx.builder.build_store(ptr_to_arr, arr_ptr);
|
||||
arr_str_ptr
|
||||
}
|
||||
|
||||
arr_str_ptr
|
||||
}
|
||||
|
||||
pub fn gen_comprehension<'ctx, 'a, G: CodeGenerator>(
|
||||
|
@ -799,9 +823,15 @@ pub fn gen_comprehension<'ctx, 'a, G: CodeGenerator>(
|
|||
) -> Result<BasicValueEnum<'ctx>, String> {
|
||||
if let ExprKind::ListComp { elt, generators } = &expr.node {
|
||||
let current = ctx.builder.get_insert_block().unwrap().get_parent().unwrap();
|
||||
let test_bb = ctx.ctx.append_basic_block(current, "test");
|
||||
let body_bb = ctx.ctx.append_basic_block(current, "body");
|
||||
let cont_bb = ctx.ctx.append_basic_block(current, "cont");
|
||||
|
||||
let init_bb = ctx.ctx.append_basic_block(current, "listcomp.init");
|
||||
let test_bb = ctx.ctx.append_basic_block(current, "listcomp.test");
|
||||
let body_bb = ctx.ctx.append_basic_block(current, "listcomp.body");
|
||||
let cont_bb = ctx.ctx.append_basic_block(current, "listcomp.cont");
|
||||
|
||||
ctx.builder.build_unconditional_branch(init_bb);
|
||||
|
||||
ctx.builder.position_at_end(init_bb);
|
||||
|
||||
let Comprehension { target, iter, ifs, .. } = &generators[0];
|
||||
let iter_val = generator.gen_expr(ctx, iter)?.unwrap().to_basic_value_enum(ctx, generator, iter.custom.unwrap())?;
|
||||
|
@ -830,10 +860,11 @@ pub fn gen_comprehension<'ctx, 'a, G: CodeGenerator>(
|
|||
// in case length is non-positive
|
||||
let is_valid =
|
||||
ctx.builder.build_int_compare(inkwell::IntPredicate::SGT, length, zero_32, "check");
|
||||
let normal = ctx.ctx.append_basic_block(current, "normal_list");
|
||||
let empty = ctx.ctx.append_basic_block(current, "empty_list");
|
||||
let list_init = ctx.ctx.append_basic_block(current, "list_init");
|
||||
let normal = ctx.ctx.append_basic_block(current, "listcomp.normal_list");
|
||||
let empty = ctx.ctx.append_basic_block(current, "listcomp.empty_list");
|
||||
let list_init = ctx.ctx.append_basic_block(current, "listcomp.list_init");
|
||||
ctx.builder.build_conditional_branch(is_valid, normal, empty);
|
||||
|
||||
// normal: allocate a list
|
||||
ctx.builder.position_at_end(normal);
|
||||
let list_a = allocate_list(
|
||||
|
@ -841,21 +872,31 @@ pub fn gen_comprehension<'ctx, 'a, G: CodeGenerator>(
|
|||
ctx,
|
||||
elem_ty,
|
||||
ctx.builder.build_int_z_extend_or_bit_cast(length, size_t, "z_ext_len"),
|
||||
Some("listcomp"),
|
||||
);
|
||||
ctx.builder.build_unconditional_branch(list_init);
|
||||
|
||||
ctx.builder.position_at_end(empty);
|
||||
let list_b = allocate_list(generator, ctx, elem_ty, zero_size_t);
|
||||
let list_b = allocate_list(
|
||||
generator,
|
||||
ctx,
|
||||
elem_ty,
|
||||
zero_size_t,
|
||||
Some("list_b")
|
||||
);
|
||||
ctx.builder.build_unconditional_branch(list_init);
|
||||
|
||||
ctx.builder.position_at_end(list_init);
|
||||
let phi = ctx.builder.build_phi(list_a.get_type(), "phi");
|
||||
phi.add_incoming(&[(&list_a, normal), (&list_b, empty)]);
|
||||
list = phi.as_basic_value().into_pointer_value();
|
||||
list_content =
|
||||
ctx.build_gep_and_load(list, &[zero_size_t, zero_32]).into_pointer_value();
|
||||
list_content = ctx.build_gep_and_load(list, &[zero_size_t, zero_32], Some("list_content"))
|
||||
.into_pointer_value();
|
||||
|
||||
let i = generator.gen_store_target(ctx, target, Some("i.addr"))?;
|
||||
ctx.builder.build_store(i, ctx.builder.build_int_sub(start, step, "start_init"));
|
||||
ctx.builder.build_unconditional_branch(test_bb);
|
||||
|
||||
ctx.builder.position_at_end(test_bb);
|
||||
let sign =
|
||||
ctx.builder.build_int_compare(inkwell::IntPredicate::SGT, step, zero_32, "sign");
|
||||
|
@ -877,32 +918,36 @@ pub fn gen_comprehension<'ctx, 'a, G: CodeGenerator>(
|
|||
body_bb,
|
||||
cont_bb,
|
||||
);
|
||||
|
||||
ctx.builder.position_at_end(body_bb);
|
||||
} else {
|
||||
let length = ctx
|
||||
.build_gep_and_load(
|
||||
iter_val.into_pointer_value(),
|
||||
&[zero_size_t, int32.const_int(1, false)],
|
||||
Some("length"),
|
||||
)
|
||||
.into_int_value();
|
||||
list = allocate_list(generator, ctx, elem_ty, length);
|
||||
list = allocate_list(generator, ctx, elem_ty, length, Some("listcomp"));
|
||||
list_content =
|
||||
ctx.build_gep_and_load(list, &[zero_size_t, zero_32]).into_pointer_value();
|
||||
ctx.build_gep_and_load(list, &[zero_size_t, zero_32], Some("list_content")).into_pointer_value();
|
||||
let counter = generator.gen_var_alloc(ctx, size_t.into(), Some("counter.addr"))?;
|
||||
// counter = -1
|
||||
ctx.builder.build_store(counter, size_t.const_int(u64::max_value(), true));
|
||||
ctx.builder.build_unconditional_branch(test_bb);
|
||||
|
||||
ctx.builder.position_at_end(test_bb);
|
||||
let tmp = ctx.builder.build_load(counter, "i").into_int_value();
|
||||
let tmp = ctx.builder.build_int_add(tmp, size_t.const_int(1, false), "inc");
|
||||
ctx.builder.build_store(counter, tmp);
|
||||
let cmp = ctx.builder.build_int_compare(inkwell::IntPredicate::SLT, tmp, length, "cmp");
|
||||
ctx.builder.build_conditional_branch(cmp, body_bb, cont_bb);
|
||||
|
||||
ctx.builder.position_at_end(body_bb);
|
||||
let arr_ptr = ctx
|
||||
.build_gep_and_load(iter_val.into_pointer_value(), &[zero_size_t, zero_32])
|
||||
.build_gep_and_load(iter_val.into_pointer_value(), &[zero_size_t, zero_32], Some("arr.addr"))
|
||||
.into_pointer_value();
|
||||
let val = ctx.build_gep_and_load(arr_ptr, &[tmp]);
|
||||
let val = ctx.build_gep_and_load(arr_ptr, &[tmp], Some("val"));
|
||||
generator.gen_assign(ctx, target, val.into())?;
|
||||
}
|
||||
for cond in ifs.iter() {
|
||||
|
@ -913,8 +958,10 @@ pub fn gen_comprehension<'ctx, 'a, G: CodeGenerator>(
|
|||
.into_int_value();
|
||||
let succ = ctx.ctx.append_basic_block(current, "then");
|
||||
ctx.builder.build_conditional_branch(result, succ, test_bb);
|
||||
|
||||
ctx.builder.position_at_end(succ);
|
||||
}
|
||||
|
||||
let elem = generator.gen_expr(ctx, elt)?.unwrap();
|
||||
let i = ctx.builder.build_load(index, "i").into_int_value();
|
||||
let elem_ptr = unsafe { ctx.builder.build_gep(list_content, &[i], "elem_ptr") };
|
||||
|
@ -923,11 +970,13 @@ pub fn gen_comprehension<'ctx, 'a, G: CodeGenerator>(
|
|||
ctx.builder
|
||||
.build_store(index, ctx.builder.build_int_add(i, size_t.const_int(1, false), "inc"));
|
||||
ctx.builder.build_unconditional_branch(test_bb);
|
||||
|
||||
ctx.builder.position_at_end(cont_bb);
|
||||
let len_ptr = unsafe {
|
||||
ctx.builder.build_gep(list, &[zero_size_t, int32.const_int(1, false)], "length")
|
||||
};
|
||||
ctx.builder.build_store(len_ptr, ctx.builder.build_load(index, "index"));
|
||||
|
||||
Ok(list.into())
|
||||
} else {
|
||||
unreachable!()
|
||||
|
@ -1070,7 +1119,7 @@ pub fn gen_expr<'ctx, 'a, G: CodeGenerator>(
|
|||
}
|
||||
}
|
||||
ExprKind::Name { id, .. } => match ctx.var_assignment.get(id) {
|
||||
Some((ptr, None, _)) => ctx.builder.build_load(*ptr, "load").into(),
|
||||
Some((ptr, None, _)) => ctx.builder.build_load(*ptr, id.to_string().as_str()).into(),
|
||||
Some((_, Some(static_value), _)) => ValueEnum::Static(static_value.clone()),
|
||||
None => {
|
||||
let resolver = ctx.resolver.clone();
|
||||
|
@ -1101,8 +1150,9 @@ pub fn gen_expr<'ctx, 'a, G: CodeGenerator>(
|
|||
elements[0].get_type()
|
||||
};
|
||||
let length = generator.get_size_type(ctx.ctx).const_int(elements.len() as u64, false);
|
||||
let arr_str_ptr = allocate_list(generator, ctx, ty, length);
|
||||
let arr_ptr = ctx.build_gep_and_load(arr_str_ptr, &[zero, zero]).into_pointer_value();
|
||||
let arr_str_ptr = allocate_list(generator, ctx, ty, length, Some("list"));
|
||||
let arr_ptr = ctx.build_gep_and_load(arr_str_ptr, &[zero, zero], Some("list.ptr.addr"))
|
||||
.into_pointer_value();
|
||||
unsafe {
|
||||
for (i, v) in elements.iter().enumerate() {
|
||||
let elem_ptr = ctx.builder.build_gep(
|
||||
|
@ -1148,6 +1198,7 @@ pub fn gen_expr<'ctx, 'a, G: CodeGenerator>(
|
|||
Ok(ValueEnum::Dynamic(ctx.build_gep_and_load(
|
||||
v.into_pointer_value(),
|
||||
&[zero, int32.const_int(index as u64, false)],
|
||||
None,
|
||||
))) as Result<_, String>
|
||||
}, Ok)?,
|
||||
ValueEnum::Dynamic(v) => {
|
||||
|
@ -1155,6 +1206,7 @@ pub fn gen_expr<'ctx, 'a, G: CodeGenerator>(
|
|||
ValueEnum::Dynamic(ctx.build_gep_and_load(
|
||||
v.into_pointer_value(),
|
||||
&[zero, int32.const_int(index as u64, false)],
|
||||
None,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
@ -1511,7 +1563,8 @@ pub fn gen_expr<'ctx, 'a, G: CodeGenerator>(
|
|||
.to_basic_value_enum(ctx, generator, value.custom.unwrap())?
|
||||
.into_pointer_value();
|
||||
let ty = ctx.get_llvm_type(generator, *ty);
|
||||
let arr_ptr = ctx.build_gep_and_load(v, &[zero, zero]).into_pointer_value();
|
||||
let arr_ptr = ctx.build_gep_and_load(v, &[zero, zero], Some("arr.addr"))
|
||||
.into_pointer_value();
|
||||
if let ExprKind::Slice { lower, upper, step } = &slice.node {
|
||||
let one = int32.const_int(1, false);
|
||||
let (start, end, step) =
|
||||
|
@ -1535,7 +1588,7 @@ pub fn gen_expr<'ctx, 'a, G: CodeGenerator>(
|
|||
.into_int_value(),
|
||||
step,
|
||||
);
|
||||
let res_array_ret = allocate_list(generator, ctx, ty, length);
|
||||
let res_array_ret = allocate_list(generator, ctx, ty, length, Some("ret"));
|
||||
let res_ind =
|
||||
handle_slice_indices(&None, &None, &None, ctx, generator, res_array_ret)?;
|
||||
list_slice_assignment(
|
||||
|
@ -1550,7 +1603,7 @@ pub fn gen_expr<'ctx, 'a, G: CodeGenerator>(
|
|||
res_array_ret.into()
|
||||
} else {
|
||||
let len = ctx
|
||||
.build_gep_and_load(v, &[zero, int32.const_int(1, false)])
|
||||
.build_gep_and_load(v, &[zero, int32.const_int(1, false)], Some("len"))
|
||||
.into_int_value();
|
||||
let raw_index = generator
|
||||
.gen_expr(ctx, slice)?
|
||||
|
@ -1590,7 +1643,7 @@ pub fn gen_expr<'ctx, 'a, G: CodeGenerator>(
|
|||
[Some(raw_index), Some(len), None],
|
||||
expr.location,
|
||||
);
|
||||
ctx.build_gep_and_load(arr_ptr, &[index]).into()
|
||||
ctx.build_gep_and_load(arr_ptr, &[index], None).into()
|
||||
}
|
||||
} else if let TypeEnum::TTuple { .. } = &*ctx.unifier.get_ty(value.custom.unwrap()) {
|
||||
let index: u32 =
|
||||
|
|
|
@ -162,7 +162,7 @@ pub fn handle_slice_indices<'a, 'ctx, G: CodeGenerator>(
|
|||
let int32 = ctx.ctx.i32_type();
|
||||
let zero = int32.const_zero();
|
||||
let one = int32.const_int(1, false);
|
||||
let length = ctx.build_gep_and_load(list, &[zero, one]).into_int_value();
|
||||
let length = ctx.build_gep_and_load(list, &[zero, one], Some("length")).into_int_value();
|
||||
let length = ctx.builder.build_int_truncate_or_bit_cast(length, int32, "leni32");
|
||||
Ok(match (start, end, step) {
|
||||
(s, e, None) => (
|
||||
|
@ -309,21 +309,21 @@ pub fn list_slice_assignment<'ctx, 'a>(
|
|||
|
||||
let zero = int32.const_zero();
|
||||
let one = int32.const_int(1, false);
|
||||
let dest_arr_ptr = ctx.build_gep_and_load(dest_arr, &[zero, zero]);
|
||||
let dest_arr_ptr = ctx.build_gep_and_load(dest_arr, &[zero, zero], Some("dest.addr"));
|
||||
let dest_arr_ptr = ctx.builder.build_pointer_cast(
|
||||
dest_arr_ptr.into_pointer_value(),
|
||||
elem_ptr_type,
|
||||
"dest_arr_ptr_cast",
|
||||
);
|
||||
let dest_len = ctx.build_gep_and_load(dest_arr, &[zero, one]).into_int_value();
|
||||
let dest_len = ctx.build_gep_and_load(dest_arr, &[zero, one], Some("dest.len")).into_int_value();
|
||||
let dest_len = ctx.builder.build_int_truncate_or_bit_cast(dest_len, int32, "srclen32");
|
||||
let src_arr_ptr = ctx.build_gep_and_load(src_arr, &[zero, zero]);
|
||||
let src_arr_ptr = ctx.build_gep_and_load(src_arr, &[zero, zero], Some("src.addr"));
|
||||
let src_arr_ptr = ctx.builder.build_pointer_cast(
|
||||
src_arr_ptr.into_pointer_value(),
|
||||
elem_ptr_type,
|
||||
"src_arr_ptr_cast",
|
||||
);
|
||||
let src_len = ctx.build_gep_and_load(src_arr, &[zero, one]).into_int_value();
|
||||
let src_len = ctx.build_gep_and_load(src_arr, &[zero, one], Some("src.len")).into_int_value();
|
||||
let src_len = ctx.builder.build_int_truncate_or_bit_cast(src_len, int32, "srclen32");
|
||||
|
||||
// index in bound and positive should be done
|
||||
|
|
|
@ -319,8 +319,7 @@ impl WorkerRegistry {
|
|||
let result = module.verify();
|
||||
if let Err(err) = result {
|
||||
println!("{}", module.print_to_string().to_str().unwrap());
|
||||
println!("{}", err.to_string());
|
||||
panic!()
|
||||
panic!("{}", err.to_string())
|
||||
}
|
||||
|
||||
let pass_options = PassBuilderOptions::create();
|
||||
|
|
|
@ -93,7 +93,7 @@ pub fn gen_store_target<'ctx, 'a, G: CodeGenerator>(
|
|||
.to_basic_value_enum(ctx, generator, value.custom.unwrap())?
|
||||
.into_pointer_value();
|
||||
let len = ctx
|
||||
.build_gep_and_load(v, &[zero, i32_type.const_int(1, false)])
|
||||
.build_gep_and_load(v, &[zero, i32_type.const_int(1, false)], Some("len"))
|
||||
.into_int_value();
|
||||
let raw_index = generator
|
||||
.gen_expr(ctx, slice)?
|
||||
|
@ -135,7 +135,7 @@ pub fn gen_store_target<'ctx, 'a, G: CodeGenerator>(
|
|||
);
|
||||
unsafe {
|
||||
let arr_ptr = ctx
|
||||
.build_gep_and_load(v, &[i32_type.const_zero(), i32_type.const_zero()])
|
||||
.build_gep_and_load(v, &[i32_type.const_zero(), i32_type.const_zero()], Some("arr.addr"))
|
||||
.into_pointer_value();
|
||||
ctx.builder.build_gep(arr_ptr, &[index], name.unwrap_or(""))
|
||||
}
|
||||
|
@ -327,6 +327,7 @@ pub fn gen_for<'ctx, 'a, G: CodeGenerator>(
|
|||
.build_gep_and_load(
|
||||
iter_val.into_pointer_value(),
|
||||
&[zero, int32.const_int(1, false)],
|
||||
Some("len")
|
||||
)
|
||||
.into_int_value();
|
||||
ctx.builder.build_unconditional_branch(test_bb);
|
||||
|
@ -338,9 +339,9 @@ pub fn gen_for<'ctx, 'a, G: CodeGenerator>(
|
|||
|
||||
ctx.builder.position_at_end(body_bb);
|
||||
let arr_ptr = ctx
|
||||
.build_gep_and_load(iter_val.into_pointer_value(), &[zero, zero])
|
||||
.build_gep_and_load(iter_val.into_pointer_value(), &[zero, zero], Some("arr.addr"))
|
||||
.into_pointer_value();
|
||||
let val = ctx.build_gep_and_load(arr_ptr, &[index]);
|
||||
let val = ctx.build_gep_and_load(arr_ptr, &[index], Some("val"));
|
||||
generator.gen_assign(ctx, target, val.into())?;
|
||||
gen_block(generator, ctx, body.iter())?;
|
||||
|
||||
|
|
|
@ -1013,6 +1013,7 @@ pub fn get_builtins(primitives: &mut (PrimitiveStore, Unifier)) -> BuiltinInfo {
|
|||
.build_gep_and_load(
|
||||
arg.into_pointer_value(),
|
||||
&[zero, int32.const_int(1, false)],
|
||||
None,
|
||||
)
|
||||
.into_int_value();
|
||||
if len.get_type().get_bit_width() != 32 {
|
||||
|
|
Loading…
Reference in New Issue