Skip to content

Commit

Permalink
Merge pull request #513 from shiika-lang/ptr-type
Browse files Browse the repository at this point in the history
Remove argument `ty` from gen.llvm_type
  • Loading branch information
yhara authored Aug 17, 2023
2 parents 3d2207a + 0de0b1e commit d02a92d
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 151 deletions.
39 changes: 11 additions & 28 deletions lib/skc_codegen/src/boxing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,25 @@ use shiika_core::{names::*, ty};
impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
/// Generate llvm funcs about boxing
pub fn gen_boxing_funcs(&self) {
let fn_type = self
.llvm_type(&ty::raw("Bool"))
.fn_type(&[self.i1_type.into()], false);
let fn_type = self.llvm_type().fn_type(&[self.i1_type.into()], false);
self.module.add_function("box_bool", fn_type, None);
let fn_type = self
.i1_type
.fn_type(&[self.llvm_type(&ty::raw("Bool")).into()], false);
let fn_type = self.i1_type.fn_type(&[self.llvm_type().into()], false);
self.module.add_function("unbox_bool", fn_type, None);
let fn_type = self
.llvm_type(&ty::raw("Int"))
.fn_type(&[self.i64_type.into()], false);
let fn_type = self.llvm_type().fn_type(&[self.i64_type.into()], false);
self.module.add_function("box_int", fn_type, None);
let fn_type = self
.i64_type
.fn_type(&[self.llvm_type(&ty::raw("Int")).into()], false);
let fn_type = self.i64_type.fn_type(&[self.llvm_type().into()], false);
self.module.add_function("unbox_int", fn_type, None);
let fn_type = self
.llvm_type(&ty::raw("Float"))
.fn_type(&[self.f64_type.into()], false);
let fn_type = self.llvm_type().fn_type(&[self.f64_type.into()], false);
self.module.add_function("box_float", fn_type, None);
let fn_type = self
.f64_type
.fn_type(&[self.llvm_type(&ty::raw("Float")).into()], false);
let fn_type = self.f64_type.fn_type(&[self.llvm_type().into()], false);
self.module.add_function("unbox_float", fn_type, None);
let fn_type = self
.llvm_type(&ty::raw("Shiika::Internal::Ptr"))
.fn_type(&[self.i8ptr_type.into()], false);
let fn_type = self.llvm_type().fn_type(&[self.ptr_type.into()], false);
self.module.add_function("box_i8ptr", fn_type, None);
let fn_type = self.i8ptr_type.fn_type(
&[self.llvm_type(&ty::raw("Shiika::Internal::Ptr")).into()],
false,
);
let fn_type = self.ptr_type.fn_type(&[self.llvm_type().into()], false);
self.module.add_function("unbox_i8ptr", fn_type, None);
let fn_type = self
.llvm_type(&ty::raw("String"))
.fn_type(&[self.i8ptr_type.into(), self.i64_type.into()], false);
.llvm_type()
.fn_type(&[self.ptr_type.into(), self.i64_type.into()], false);
self.module
.add_function("gen_literal_string", fn_type, None);
}
Expand Down Expand Up @@ -132,7 +115,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
ty::raw("Shiika::Internal::Ptr"),
function.get_params()[0].into_pointer_value(),
);
let i8ptr = self.build_ivar_load_raw(sk_ptr, self.i8ptr_type.into(), 0, "@llvm_i8ptr");
let i8ptr = self.build_ivar_load_raw(sk_ptr, self.ptr_type.into(), 0, "@llvm_i8ptr");
self.builder.build_return(Some(&i8ptr));

// gen_literal_string
Expand Down
39 changes: 13 additions & 26 deletions lib/skc_codegen/src/gen_exprs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
// AndEnd:
self.builder.position_at_end(merge_block);

let phi_node = self
.builder
.build_phi(self.llvm_type(&ty::raw("Bool")), "AndResult");
let phi_node = self.builder.build_phi(self.llvm_type(), "AndResult");
phi_node.add_incoming(&[
(&left_value.0, begin_block_end),
(&right_value.0, more_block_end),
Expand Down Expand Up @@ -225,9 +223,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
// OrEnd:
self.builder.position_at_end(merge_block);

let phi_node = self
.builder
.build_phi(self.llvm_type(&ty::raw("Bool")), "OrResult");
let phi_node = self.builder.build_phi(self.llvm_type(), "OrResult");
phi_node.add_incoming(&[
(&left_value.0, begin_block_end),
(&right_value.0, else_block_end),
Expand Down Expand Up @@ -278,7 +274,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
(None, else_value) => Ok(else_value),
(then_value, None) => Ok(then_value),
(Some(then_val), Some(else_val)) => {
let phi_node = self.builder.build_phi(self.llvm_type(ty), "ifResult");
let phi_node = self.builder.build_phi(self.llvm_type(), "ifResult");
phi_node
.add_incoming(&[(&then_val.0, then_block_end), (&else_val.0, else_block_end)]);
Ok(Some(SkObj::new(ty.clone(), phi_node.as_basic_value())))
Expand Down Expand Up @@ -336,9 +332,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
} else {
// MatchEnd:
self.builder.position_at_end(merge_block);
let phi_node = self
.builder
.build_phi(self.llvm_type(result_ty), "matchResult");
let phi_node = self.builder.build_phi(self.llvm_type(), "matchResult");
phi_node.add_incoming(
incoming_values
.iter()
Expand Down Expand Up @@ -679,7 +673,6 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
ret_ty: &TermTy,
) -> Result<Option<SkObj<'run>>> {
let lambda_obj = self.gen_expr(ctx, lambda_expr)?.unwrap();
let n_args = arg_exprs.len();

// Prepare arguments
let mut args = vec![lambda_obj.clone()];
Expand All @@ -698,14 +691,10 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
.append_basic_block(ctx.function, "Invoke_lambda_end");

// Create the type of lambda_xx()
let fn_x_ty = ty::raw(&format!("Fn{}", n_args));
let fn_x_type = self.llvm_type(&fn_x_ty);
let mut arg_types = vec![fn_x_type.into()];
for e in arg_exprs {
arg_types.push(self.llvm_type(&e.ty).into());
}
let fntype = self.llvm_type(ret_ty).fn_type(&arg_types, false);
let fnptype = fntype.ptr_type(Default::default());
let n = arg_exprs.len() + 1; // +1 for self
let arg_types = vec![self.llvm_type().into(); n];
let fntype = self.llvm_type().fn_type(&arg_types, false);
let fnptype = self.ptr_type;

// Cast `fnptr` to that type
let fnptr = self.unbox_i8ptr(self.build_ivar_load(
Expand Down Expand Up @@ -849,7 +838,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
.unwrap_or_else(|| panic!("[BUG] lvar `{}' not found in ctx.lvars", name));
SkObj::new(
ty.clone(),
self.builder.build_load(self.llvm_type(ty), *ptr, name),
self.builder.build_load(self.llvm_type(), *ptr, name),
)
}

Expand Down Expand Up @@ -910,7 +899,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {

pub fn gen_const_ref(&'run self, fullname: &ConstFullname, ty: &TermTy) -> SkObj<'run> {
let name = llvm_const_name(fullname);
let llvm_type = self.llvm_type(ty);
let llvm_type = self.llvm_type();
let ptr = self
.module
.get_global(&name)
Expand Down Expand Up @@ -945,7 +934,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
.get_llvm_func(&func_name)
.as_global_value()
.as_basic_value_enum();
let fnptr_i8 = self.builder.build_bitcast(fnptr, self.i8ptr_type, "");
let fnptr_i8 = self.builder.build_bitcast(fnptr, self.ptr_type, "");
let sk_ptr = self.box_i8ptr(fnptr_i8);
let the_self = self.gen_self_expression(ctx, &ty::raw("Object"));
let captured = self._gen_lambda_captures(ctx, name, captures);
Expand Down Expand Up @@ -1039,9 +1028,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {
.get_global(&format!("str_{}", idx))
.unwrap_or_else(|| panic!("[BUG] global for str_{} not created", idx))
.as_pointer_value();
let i8ptr = self
.builder
.build_bitcast(byte_ary, self.i8ptr_type, "i8ptr");
let i8ptr = self.builder.build_bitcast(byte_ary, self.ptr_type, "i8ptr");
let bytesize = self
.i64_type
.const_int(self.str_literals[*idx].len() as u64, false);
Expand Down Expand Up @@ -1116,7 +1103,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> {

let captures = self._gen_get_lambda_captures(ctx);
let value = self.gen_expr(ctx, rhs)?.unwrap();
captures.reassign(self, *idx_in_captures, value.clone(), &rhs.ty);
captures.reassign(self, *idx_in_captures, value.clone());

let block = self.context.append_basic_block(
ctx.function,
Expand Down
26 changes: 7 additions & 19 deletions lib/skc_codegen/src/lambda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,35 +129,23 @@ impl<'run> LambdaCapture<'run> {
.build_llvm_struct_ref_raw(
&self.struct_type(gen),
self.to_struct_ptr(),
gen.i8ptr_type.clone().as_basic_type_enum(),
gen.ptr_type.clone().as_basic_type_enum(),
idx,
"load",
)
.into_pointer_value();
let pointee_ty = gen.llvm_type(ty).as_basic_type_enum();
let pointee_ty = gen.llvm_type().as_basic_type_enum();
gen.builder.build_load(pointee_ty, addr, "deref")
} else {
gen.build_llvm_struct_ref(
&self.struct_type(gen),
self.to_struct_ptr(),
ty,
idx,
"load",
)
gen.build_llvm_struct_ref(&self.struct_type(gen), self.to_struct_ptr(), idx, "load")
};
SkObj::new(ty.clone(), v)
}

/// Given there is a pointer stored at `idx`, update its value.
pub fn reassign(&self, gen: &CodeGen<'_, 'run, '_>, idx: usize, value: SkObj, ty: &TermTy) {
pub fn reassign(&self, gen: &CodeGen<'_, 'run, '_>, idx: usize, value: SkObj) {
let ptr = gen
.build_llvm_struct_ref(
&self.struct_type(gen),
self.to_struct_ptr(),
ty,
idx,
"load",
)
.build_llvm_struct_ref(&self.struct_type(gen), self.to_struct_ptr(), idx, "load")
.into_pointer_value();
gen.builder.build_store(ptr, value.0);
}
Expand Down Expand Up @@ -453,12 +441,12 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> {

fn capture_ty(&self, cap: &HirLambdaCapture) -> inkwell::types::BasicTypeEnum {
if cap.readonly {
self.llvm_type(&cap.ty)
self.llvm_type()
} else {
// The (local) variable is captured by reference.
// PERF: not needed to be by-ref when the variable is declared with
// `var` but not reassigned from closure.
self.llvm_type(&cap.ty)
self.llvm_type()
.ptr_type(Default::default())
.as_basic_type_enum()
}
Expand Down
Loading

0 comments on commit d02a92d

Please sign in to comment.