diff --git a/lib/skc_codegen/src/boxing.rs b/lib/skc_codegen/src/boxing.rs index d22774fb..fc766cb2 100644 --- a/lib/skc_codegen/src/boxing.rs +++ b/lib/skc_codegen/src/boxing.rs @@ -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); } @@ -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 diff --git a/lib/skc_codegen/src/gen_exprs.rs b/lib/skc_codegen/src/gen_exprs.rs index cfa87992..8b3467f7 100644 --- a/lib/skc_codegen/src/gen_exprs.rs +++ b/lib/skc_codegen/src/gen_exprs.rs @@ -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), @@ -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), @@ -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()))) @@ -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() @@ -679,7 +673,6 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> { ret_ty: &TermTy, ) -> Result>> { 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()]; @@ -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( @@ -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), ) } @@ -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) @@ -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); @@ -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); @@ -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, diff --git a/lib/skc_codegen/src/lambda.rs b/lib/skc_codegen/src/lambda.rs index 97f2ee63..35ad7eca 100644 --- a/lib/skc_codegen/src/lambda.rs +++ b/lib/skc_codegen/src/lambda.rs @@ -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); } @@ -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() } diff --git a/lib/skc_codegen/src/lib.rs b/lib/skc_codegen/src/lib.rs index 78edc14c..92db4056 100644 --- a/lib/skc_codegen/src/lib.rs +++ b/lib/skc_codegen/src/lib.rs @@ -37,7 +37,7 @@ pub struct CodeGen<'hir: 'ictx, 'run, 'ictx: 'run> { pub builder: &'run inkwell::builder::Builder<'ictx>, pub i1_type: inkwell::types::IntType<'ictx>, pub i8_type: inkwell::types::IntType<'ictx>, - pub i8ptr_type: inkwell::types::PointerType<'ictx>, + pub ptr_type: inkwell::types::PointerType<'ictx>, pub i32_type: inkwell::types::IntType<'ictx>, pub i64_type: inkwell::types::IntType<'ictx>, pub f64_type: inkwell::types::FloatType<'ictx>, @@ -92,7 +92,7 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { builder, i1_type: context.bool_type(), i8_type: context.i8_type(), - i8ptr_type: context.i8_type().ptr_type(Default::default()), + ptr_type: context.i8_type().ptr_type(Default::default()), i32_type: context.i32_type(), i64_type: context.i64_type(), f64_type: context.f64_type(), @@ -136,16 +136,16 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { fn gen_declares(&self) { let fn_type = self.void_type.fn_type(&[], false); self.module.add_function("GC_init", fn_type, None); - let fn_type = self.i8ptr_type.fn_type(&[self.i64_type.into()], false); + let fn_type = self.ptr_type.fn_type(&[self.i64_type.into()], false); self.module.add_function("shiika_malloc", fn_type, None); let fn_type = self - .i8ptr_type - .fn_type(&[self.i8ptr_type.into(), self.i64_type.into()], false); + .ptr_type + .fn_type(&[self.ptr_type.into(), self.i64_type.into()], false); self.module.add_function("shiika_realloc", fn_type, None); - let fn_type = self.i8ptr_type.fn_type( + let fn_type = self.ptr_type.fn_type( &[ - self.i8ptr_type.into(), + self.ptr_type.into(), self.i64_type.into(), self.i64_type.into(), ], @@ -154,11 +154,11 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { self.module .add_function("shiika_lookup_wtable", fn_type, None); - let fn_type = self.i8ptr_type.fn_type( + let fn_type = self.ptr_type.fn_type( &[ - self.i8ptr_type.into(), + self.ptr_type.into(), self.i64_type.into(), - self.i8ptr_type.into(), + self.ptr_type.into(), self.i64_type.into(), ], false, @@ -206,16 +206,16 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { fn gen_import_vtables(&self, vtables: &VTables) { for (fullname, vtable) in vtables.iter() { let name = llvm_vtable_const_name(fullname); - let ary_type = self.i8ptr_type.array_type(vtable.size() as u32); + let ary_type = self.ptr_type.array_type(vtable.size() as u32); let _global = self.module.add_global(ary_type, None, &name); } } /// Declare `external global` for each imported constant fn gen_import_constants(&self, imported_constants: &HashMap) { - for (fullname, ty) in imported_constants { + for (fullname, _) in imported_constants { let name = llvm_const_name(fullname); - let global = self.module.add_global(self.llvm_type(ty), None, &name); + let global = self.module.add_global(self.llvm_type(), None, &name); global.set_linkage(inkwell::module::Linkage::External); // @init_::XX let fn_type = self.void_type.fn_type(&[], false); @@ -228,7 +228,7 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { fn gen_vtables(&self) { for (class_fullname, vtable) in self.vtables.iter() { let method_names = vtable.to_vec(); - let ary_type = self.i8ptr_type.array_type(method_names.len() as u32); + let ary_type = self.ptr_type.array_type(method_names.len() as u32); let tmp = llvm_vtable_const_name(class_fullname); let global = self.module.add_global(ary_type, None, &tmp); global.set_constant(true); @@ -240,11 +240,11 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { .as_global_value() .as_pointer_value(); self.builder - .build_bitcast(func, self.i8ptr_type, "") + .build_bitcast(func, self.ptr_type, "") .into_pointer_value() }) .collect::>(); - global.set_initializer(&self.i8ptr_type.const_array(&func_ptrs)); + global.set_initializer(&self.ptr_type.const_array(&func_ptrs)); } } @@ -405,7 +405,7 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { struct_type.set_body(&[vt, ct, self.i1_type.into()], false); } "Shiika::Internal::Ptr" => { - struct_type.set_body(&[vt, ct, self.i8ptr_type.into()], false); + struct_type.set_body(&[vt, ct, self.ptr_type.into()], false); } _ => { struct_type.set_body(&self.llvm_field_types(&class.ivars), false); @@ -426,10 +426,7 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { ) -> Vec { let mut values = ivars.values().collect::>(); values.sort_by_key(|ivar| ivar.idx); - let mut types = values - .iter() - .map(|ivar| self.llvm_type(&ivar.ty)) - .collect::>(); + let mut types = vec![self.llvm_type(); values.len()]; types.insert(0, self.llvm_vtable_ref_type().into()); types.insert(1, self.class_object_ref_type().into()); types @@ -457,10 +454,10 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { /// Generate llvm global that holds Shiika constants fn gen_constant_ptrs(&self, constants: &HashMap) { - for (fullname, ty) in constants { + for (fullname, _) in constants { let name = llvm_const_name(fullname); - let global = self.module.add_global(self.llvm_type(ty), None, &name); - let null = self.llvm_type(ty).into_pointer_type().const_null(); + let global = self.module.add_global(self.llvm_type(), None, &name); + let null = self.llvm_type().into_pointer_type().const_null(); global.set_initializer(&null); } } @@ -536,20 +533,17 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { param_tys: &[T], ret_ty: &TermTy, ) -> inkwell::types::FunctionType<'ictx> { - let mut arg_types = param_tys - .iter() - .map(|ty| self.llvm_type(ty.as_ref()).into()) - .collect::>(); + let mut arg_types = vec![self.llvm_type().into(); param_tys.len()]; // Methods takes the self as the first argument - if let Some(ty) = self_ty { - arg_types.insert(0, self.llvm_type(ty).into()); + if self_ty.is_some() { + arg_types.insert(0, self.llvm_type().into()); } if ret_ty.is_never_type() { // `Never` does not have an instance self.void_type.fn_type(&arg_types, false) } else { - self.llvm_type(ret_ty).fn_type(&arg_types, false) + self.llvm_type().fn_type(&arg_types, false) } } @@ -800,8 +794,8 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { let alloca_start = self.context.append_basic_block(function, "alloca"); self.builder.build_unconditional_branch(alloca_start); self.builder.position_at_end(alloca_start); - for HirLVar { name, ty, captured } in lvars { - let obj_ty = self.llvm_type(ty); + for HirLVar { name, captured, .. } in lvars { + let obj_ty = self.llvm_type(); if *captured { // Allocate memory on heap in case it lives longer than the method call. let ptrptr = self.allocate_llvm_obj(&obj_ty).into_pointer_value(); @@ -845,7 +839,7 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { self.builder.build_return(None); } else if last_value.is_none() && ctx.returns.is_empty() { // `exprs` ends with `panic` and there is no `return` - let null = self.llvm_type(ret_ty).into_pointer_type().const_null(); + let null = self.llvm_type().into_pointer_type().const_null(); self.builder.build_return(Some(&null)); } else if ret_ty.is_void_type() { self.build_return_void(); @@ -861,9 +855,7 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { v = last_value.unwrap(); incomings.push((&v.0, b)); } - let phi_node = self - .builder - .build_phi(self.llvm_type(ret_ty), "methodResult"); + let phi_node = self.builder.build_phi(self.llvm_type(), "methodResult"); phi_node.add_incoming(incomings.as_slice()); self.builder.build_return(Some(&phi_node.as_basic_value())); } @@ -872,12 +864,12 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { /// LLVM type of a reference to a vtable fn llvm_vtable_ref_type(&self) -> inkwell::types::PointerType { - self.i8ptr_type + self.ptr_type } /// LLVM type of a reference to a class object fn class_object_ref_type(&self) -> inkwell::types::PointerType { - self.llvm_type(&ty::raw("Class")).into_pointer_type() + self.llvm_type().into_pointer_type() } /// Generate body of `.new` diff --git a/lib/skc_codegen/src/utils.rs b/lib/skc_codegen/src/utils.rs index 0eb7aca2..40fb92c7 100644 --- a/lib/skc_codegen/src/utils.rs +++ b/lib/skc_codegen/src/utils.rs @@ -39,7 +39,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> { idx: usize, name: &str, ) -> SkObj<'run> { - let value = self.build_ivar_load_raw(obj, self.llvm_type(&item_ty), idx, name); + let value = self.build_ivar_load_raw(obj, self.llvm_type(), idx, name); SkObj::new(item_ty, value) } @@ -88,7 +88,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> { /// Get the class object of an object as `*Class` pub fn get_class_of_obj(&self, object: SkObj<'run>) -> SkClassObj<'run> { SkClassObj( - self.build_object_struct_ref(object, &ty::raw("Class"), OBJ_CLASS_IDX, "class") + self.build_object_struct_ref(object, OBJ_CLASS_IDX, "class") .into_pointer_value(), ) } @@ -125,12 +125,11 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> { pub fn build_object_struct_ref( &self, object: SkObj<'run>, - item_ty: &TermTy, idx: usize, name: &str, ) -> inkwell::values::BasicValueEnum<'run> { let struct_ty = self.llvm_struct_type(object.ty()); - self.build_llvm_struct_ref(&struct_ty, object.0, item_ty, idx, name) + self.build_llvm_struct_ref(&struct_ty, object.0, idx, name) } /// Load a raw llvm value in a Shiika object @@ -151,11 +150,10 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> { &self, struct_ty: &inkwell::types::StructType<'run>, struct_ptr: inkwell::values::PointerValue<'run>, - item_ty: &TermTy, idx: usize, name: &str, ) -> inkwell::values::BasicValueEnum<'run> { - self.build_llvm_struct_ref_raw(struct_ty, struct_ptr, self.llvm_type(item_ty), idx, name) + self.build_llvm_struct_ref_raw(struct_ty, struct_ptr, self.llvm_type(), idx, name) } /// Get a value in an llvm struct @@ -177,7 +175,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> { ) .unwrap_or_else(|_| { panic!( - "build_llvm_struct_ref: elem not found (idx: {}, name: {}, struct_ptr: {:?})", + "build_llvm_struct_ref_raw: elem not found (idx: {}, name: {}, struct_ptr: {:?})", &idx, &name, &struct_ptr ) }); @@ -238,7 +236,7 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> { .get_global(&class_const_name) .unwrap_or_else(|| panic!("global `{}' not found", class_const_name)) .as_pointer_value(); - let t = self.llvm_type(&ty::raw("Class")); + let t = self.llvm_type(); SkClassObj( self.builder .build_load(t, class_obj_addr, "class_obj") @@ -347,25 +345,23 @@ impl<'hir, 'run, 'ictx> CodeGen<'hir, 'run, 'ictx> { /// Cast an object to different Shiika type pub fn bitcast(&self, obj: SkObj<'run>, ty: &TermTy, reg_name: &str) -> SkObj<'run> { - //debug_assert!(!self.obviously_wrong_bitcast(&obj.0, &self.llvm_type(ty))); + //debug_assert!(!self.obviously_wrong_bitcast(&obj.0, &self.llvm_type())); SkObj::new( ty.clone(), self.builder - .build_bitcast(obj.0, self.llvm_type(ty), reg_name), + .build_bitcast(obj.0, self.llvm_type(), reg_name), ) } /// Create `%Foo* null` pub fn null_ptr(&self, ty: &TermTy) -> SkObj<'run> { - let ptr = self.llvm_type(ty).into_pointer_type().const_null(); + let ptr = self.llvm_type().into_pointer_type().const_null(); SkObj::new(ty.clone(), ptr) } /// LLVM type of a Shiika object - pub fn llvm_type(&self, ty: &TermTy) -> inkwell::types::BasicTypeEnum<'ictx> { - self.llvm_struct_type(ty) - .ptr_type(Default::default()) - .as_basic_type_enum() + pub fn llvm_type(&self) -> inkwell::types::BasicTypeEnum<'ictx> { + self.ptr_type.as_basic_type_enum() } /// LLVM struct type of a Shiika object diff --git a/lib/skc_codegen/src/values.rs b/lib/skc_codegen/src/values.rs index 414e8980..cd43781e 100644 --- a/lib/skc_codegen/src/values.rs +++ b/lib/skc_codegen/src/values.rs @@ -17,10 +17,10 @@ impl<'run> SkObj<'run> { /// Returns a null pointer cast to `%Object*`. pub fn nullptr(gen: &CodeGen<'_, 'run, '_>) -> SkObj<'run> { let ty = ty::raw("Object"); - let null = gen.i8ptr_type.const_null().as_basic_value_enum(); + let null = gen.ptr_type.const_null().as_basic_value_enum(); SkObj::new( ty.clone(), - gen.builder.build_bitcast(null, gen.llvm_type(&ty), "as"), + gen.builder.build_bitcast(null, gen.llvm_type(), "as"), ) } @@ -44,7 +44,7 @@ impl<'run> SkObj<'run> { ) -> inkwell::values::BasicValueEnum<'run> { code_gen .builder - .build_bitcast(self.0, code_gen.i8ptr_type, "ptr") + .build_bitcast(self.0, code_gen.ptr_type, "ptr") } pub fn struct_ty(&self, gen: &'run CodeGen<'_, 'run, '_>) -> inkwell::types::StructType<'run> { @@ -59,11 +59,10 @@ pub struct SkClassObj<'run>(pub inkwell::values::PointerValue<'run>); impl<'run> SkClassObj<'run> { /// Returns a null pointer cast to `%Object*`. pub fn nullptr(gen: &CodeGen<'_, 'run, '_>) -> SkClassObj<'run> { - let ty = ty::raw("Class"); - let null = gen.i8ptr_type.const_null().as_basic_value_enum(); + let null = gen.ptr_type.const_null().as_basic_value_enum(); SkClassObj( gen.builder - .build_bitcast(null, gen.llvm_type(&ty), "as") + .build_bitcast(null, gen.llvm_type(), "as") .into_pointer_value(), ) } @@ -86,7 +85,7 @@ impl<'run> I8Ptr<'run> { ) -> I8Ptr<'run> { I8Ptr( gen.builder - .build_bitcast(p, gen.i8ptr_type, "cast") + .build_bitcast(p, gen.ptr_type, "cast") .into_pointer_value(), ) } diff --git a/lib/skc_codegen/src/vtable.rs b/lib/skc_codegen/src/vtable.rs index 4f64babb..c8160a05 100644 --- a/lib/skc_codegen/src/vtable.rs +++ b/lib/skc_codegen/src/vtable.rs @@ -17,15 +17,11 @@ impl<'run> VTableRef<'run> { VTableRef { ptr, len } } - fn llvm_type(gen: &CodeGen<'_, 'run, '_>, len: usize) -> inkwell::types::PointerType<'run> { - Self::llvm_pointee_type(gen, len).ptr_type(Default::default()) - } - fn llvm_pointee_type( gen: &CodeGen<'_, 'run, '_>, len: usize, ) -> inkwell::types::ArrayType<'run> { - gen.i8ptr_type.array_type(len as u32) + gen.ptr_type.array_type(len as u32) } /// Returns the vtable of a Shiika object. @@ -34,7 +30,7 @@ impl<'run> VTableRef<'run> { object: SkObj<'run>, len: usize, ) -> VTableRef<'run> { - let item_ty = Self::llvm_type(gen, len).as_basic_type_enum(); + let item_ty = gen.ptr_type.as_basic_type_enum(); let ptr = gen .build_object_struct_ref_raw(object, item_ty, OBJ_VTABLE_IDX, "vtable") .into_pointer_value(); @@ -79,7 +75,7 @@ impl<'run> OpaqueVTableRef<'run> { SkObj::new( ty.clone(), gen.builder - .build_bitcast(self.ptr.as_basic_value_enum(), gen.llvm_type(&ty), "as"), + .build_bitcast(self.ptr.as_basic_value_enum(), gen.llvm_type(), "as"), ) } } diff --git a/lib/skc_codegen/src/wtable.rs b/lib/skc_codegen/src/wtable.rs index 99e50686..6fa5a2b8 100644 --- a/lib/skc_codegen/src/wtable.rs +++ b/lib/skc_codegen/src/wtable.rs @@ -7,30 +7,26 @@ use skc_hir::SkClass; /// Define llvm constants like `@shiika_wtable_Array_Enumerable` pub fn gen_wtable_constants(code_gen: &CodeGen, sk_class: &SkClass) { for (mod_name, method_names) in &sk_class.wtable.0 { - let ary_type = code_gen.i8ptr_type.array_type(method_names.len() as u32); + let ary_type = code_gen.ptr_type.array_type(method_names.len() as u32); let cname = llvm_wtable_const_name(&sk_class.fullname(), mod_name); let global = code_gen.module.add_global(ary_type, None, &cname); global.set_constant(true); let func_ptrs = method_names .iter() .map(|name| { - let func = code_gen + code_gen .get_llvm_func(&method_func_name(name)) .as_global_value() - .as_pointer_value(); - code_gen - .builder - .build_bitcast(func, code_gen.i8ptr_type, "") - .into_pointer_value() + .as_pointer_value() }) .collect::>(); - global.set_initializer(&code_gen.i8ptr_type.const_array(&func_ptrs)); + global.set_initializer(&code_gen.ptr_type.const_array(&func_ptrs)); } } /// Define `@insert_XX_wtables()` for the class pub fn gen_insert_wtable(code_gen: &CodeGen, sk_class: &SkClass) { - let fargs = &[code_gen.llvm_type(&ty::raw("Class")).into()]; + let fargs = &[code_gen.llvm_type().into()]; let ftype = code_gen.void_type.fn_type(fargs, false); let fname = insert_wtable_func_name(&sk_class.fullname()); let function = code_gen.module.add_function(&fname, ftype, None); @@ -72,7 +68,7 @@ fn load_wtable_const<'a>( }); code_gen .builder - .build_bitcast(ptr, code_gen.i8ptr_type, "ary") + .build_bitcast(ptr, code_gen.ptr_type, "ary") } /// Name of llvm constant of a wtable