@@ -988,6 +988,9 @@ impl<'a, 'ctx> LLVMBuilder<'a, 'ctx> {
988988 . add_function ( & llvmname, fn_type, Some ( Linkage :: External ) )
989989 }
990990
991+ /// # get_fn_type
992+ ///
993+ /// get_fn_type returns the inkwell function type for a pivot-lang function value.
991994 fn get_fn_type ( & self , fnvalue : & FNValue , ctx : & mut Ctx < ' a > ) -> FunctionType < ' ctx > {
992995 ctx. protect_generic_context ( & fnvalue. fntype . generic_map , |ctx| {
993996 ctx. run_in_type_mod ( fnvalue, |ctx, fnvalue| {
@@ -1017,6 +1020,7 @@ impl<'a, 'ctx> LLVMBuilder<'a, 'ctx> {
10171020 !fnvalue. is_declare && fnvalue. name != "main" ,
10181021 )
10191022 . fn_type ( & param_types, false ) ;
1023+
10201024 fn_type
10211025 } )
10221026 } )
@@ -1570,14 +1574,16 @@ impl<'a, 'ctx> LLVMBuilder<'a, 'ctx> {
15701574 }
15711575 }
15721576
1573- /// try get function value from module
1577+ /// # get_or_insert_fn
15741578 ///
1575- /// if not found, create a declaration
1579+ /// it returns the emitted function from llvm if it exists,
1580+ /// otherwise it will emit the function value and the return the new value.
15761581 ///
1577- /// bool: 是否已经实现过该函数
1582+ /// The bool means whether the function exists in the llvm builder or not.
15781583 fn get_or_insert_fn ( & self , pltp : & FNValue , ctx : & mut Ctx < ' a > ) -> ( FunctionValue < ' ctx > , bool ) {
1579- let llvmname = pltp. append_name_with_generic ( pltp. llvmname . clone ( ) ) ;
1580- if let Some ( v) = self . module . get_function ( & llvmname) {
1584+ let final_llvm_name = pltp. append_name_with_generic ( pltp. llvmname . clone ( ) ) ;
1585+
1586+ if let Some ( v) = self . module . get_function ( & final_llvm_name) {
15811587 return ( v, v. count_basic_blocks ( ) != 0 ) ;
15821588 }
15831589 let fn_type = self . get_fn_type ( pltp, ctx) ;
@@ -1587,7 +1593,9 @@ impl<'a, 'ctx> LLVMBuilder<'a, 'ctx> {
15871593 } else {
15881594 Linkage :: Private
15891595 } ;
1590- let f = self . module . add_function ( & llvmname, fn_type, Some ( linkage) ) ;
1596+ let f = self
1597+ . module
1598+ . add_function ( & final_llvm_name, fn_type, Some ( linkage) ) ;
15911599 if !pltp. is_declare {
15921600 f. set_call_conventions ( CALL_CONV ) ;
15931601 }
@@ -1801,14 +1809,17 @@ impl<'a, 'ctx> IRBuilder<'a, 'ctx> for LLVMBuilder<'a, 'ctx> {
18011809 . yield_ctx_handle = ctx_handle;
18021810 self . builder . position_at_end ( prev_bb) ;
18031811 }
1812+ /// # bitcast
1813+ ///
1814+ /// it casts the origin handle into the pointer value
18041815 fn bitcast (
18051816 & self ,
18061817 _ctx : & mut Ctx < ' a > ,
1807- from : ValueHandle ,
1818+ origin : ValueHandle ,
18081819 _to : & PLType ,
18091820 _name : & str ,
18101821 ) -> ValueHandle {
1811- let lv = self . get_llvm_value ( from ) . unwrap ( ) ;
1822+ let lv = self . get_llvm_value ( origin ) . unwrap ( ) ;
18121823 let re = if lv. is_function_value ( ) {
18131824 lv. into_function_value ( )
18141825 . as_global_value ( )
@@ -2058,6 +2069,10 @@ impl<'a, 'ctx> IRBuilder<'a, 'ctx> for LLVMBuilder<'a, 'ctx> {
20582069 fn_value. set_call_conventions ( CALL_CONV ) ;
20592070 self . get_llvm_value_handle ( & fn_value. as_any_value_enum ( ) )
20602071 }
2072+
2073+ /// # opaque_struct_type
2074+ ///
2075+ /// it creates an opaque StructType with no type definition yet defined.
20612076 fn opaque_struct_type ( & self , name : & str ) {
20622077 self . context . opaque_struct_type ( name) ;
20632078 }
@@ -2621,24 +2636,28 @@ impl<'a, 'ctx> IRBuilder<'a, 'ctx> for LLVMBuilder<'a, 'ctx> {
26212636 Ok ( ( ) )
26222637 }
26232638
2639+ /// # build_sub_program_by_pltp
2640+ ///
2641+ /// it creates a subprogram for a closure, which helps to debug the closure
26242642 fn build_sub_program_by_pltp (
26252643 & self ,
2626- paralist : & [ Arc < RefCell < PLType > > ] ,
2627- ret : Arc < RefCell < PLType > > ,
2644+ param_tps : & [ Arc < RefCell < PLType > > ] ,
2645+ ret_tp : Arc < RefCell < PLType > > ,
26282646 name : & str ,
26292647 start_line : u32 ,
2630- fnvalue : ValueHandle ,
2648+ closure_fn_value : ValueHandle ,
26312649 child : & mut Ctx < ' a > ,
26322650 ) {
2633- let mut param_ditypes = vec ! [ ] ;
2634- for pltype in paralist. iter ( ) {
2635- param_ditypes. push ( self . get_ditype ( & pltype. borrow ( ) , child) . unwrap ( ) ) ;
2651+ // debug information about types
2652+ let mut param_di_types = vec ! [ ] ;
2653+ for pltype in param_tps. iter ( ) {
2654+ param_di_types. push ( self . get_ditype ( & pltype. borrow ( ) , child) . unwrap ( ) ) ;
26362655 }
26372656 // debug info
26382657 let subroutine_type = self . dibuilder . create_subroutine_type (
26392658 self . get_cur_di_file ( ) ,
2640- self . get_ditype ( & ret . borrow ( ) , child) ,
2641- & param_ditypes ,
2659+ self . get_ditype ( & ret_tp . borrow ( ) , child) ,
2660+ & param_di_types ,
26422661 DIFlags :: PUBLIC ,
26432662 ) ;
26442663 let subprogram = self . dibuilder . create_function (
@@ -2654,9 +2673,11 @@ impl<'a, 'ctx> IRBuilder<'a, 'ctx> for LLVMBuilder<'a, 'ctx> {
26542673 DIFlags :: PUBLIC ,
26552674 false ,
26562675 ) ;
2657- let funcvalue = self . get_llvm_value ( fnvalue) . unwrap ( ) . into_function_value ( ) ;
2676+ let funcvalue = self
2677+ . get_llvm_value ( closure_fn_value)
2678+ . unwrap ( )
2679+ . into_function_value ( ) ;
26582680 funcvalue. set_subprogram ( subprogram) ;
2659- // let discope = child.discope;
26602681 self . discope . set ( subprogram. as_debug_info_scope ( ) ) ;
26612682 }
26622683 fn build_return ( & self , v : Option < ValueHandle > ) {
@@ -2668,6 +2689,10 @@ impl<'a, 'ctx> IRBuilder<'a, 'ctx> for LLVMBuilder<'a, 'ctx> {
26682689 self . builder . build_return ( None ) . unwrap ( ) ;
26692690 }
26702691 }
2692+
2693+ /// # create_closure_variable_dbg
2694+ ///
2695+ /// it creates the debug information for the closure variables
26712696 #[ allow( clippy:: too_many_arguments) ]
26722697 fn create_closure_variable_dbg (
26732698 & self ,
@@ -2679,7 +2704,7 @@ impl<'a, 'ctx> IRBuilder<'a, 'ctx> for LLVMBuilder<'a, 'ctx> {
26792704 allocab : BlockHandle ,
26802705 name : & str ,
26812706 ) {
2682- let divar = self . dibuilder . create_parameter_variable (
2707+ let di_var = self . dibuilder . create_parameter_variable (
26832708 self . discope . get ( ) ,
26842709 name,
26852710 i as u32 ,
@@ -2697,12 +2722,11 @@ impl<'a, 'ctx> IRBuilder<'a, 'ctx> for LLVMBuilder<'a, 'ctx> {
26972722 . try_into ( )
26982723 . unwrap ( ) ;
26992724 let raw_tp = v. get_type ( ) ;
2700- // self.builder.position_at_end(allocab);
27012725 let alloca = self . builder . build_alloca ( raw_tp, "para" ) . unwrap ( ) ;
27022726 self . builder . build_store ( alloca, v) . unwrap ( ) ;
27032727 self . dibuilder . insert_declare_at_end (
27042728 alloca,
2705- Some ( divar ) ,
2729+ Some ( di_var ) ,
27062730 None ,
27072731 self . builder . get_current_debug_location ( ) . unwrap ( ) ,
27082732 allocab,
@@ -2921,6 +2945,9 @@ impl<'a, 'ctx> IRBuilder<'a, 'ctx> for LLVMBuilder<'a, 'ctx> {
29212945 self . get_llvm_value_handle ( & global. as_any_value_enum ( ) )
29222946 }
29232947
2948+ /// # gen_st_visit_function
2949+ ///
2950+ /// it generates the visit function for each structure field for GC use.
29242951 fn gen_st_visit_function (
29252952 & self ,
29262953 ctx : & mut Ctx < ' a > ,
@@ -3134,6 +3161,11 @@ impl<'a, 'ctx> IRBuilder<'a, 'ctx> for LLVMBuilder<'a, 'ctx> {
31343161 self . get_llvm_value_handle ( & funcvalue. get_nth_param ( i) . unwrap ( ) . as_any_value_enum ( ) ) ,
31353162 ) ;
31363163 }
3164+
3165+ /// # create_closure_fn
3166+ ///
3167+ /// create_closure_fn creates a function type in LLVM format and then emit it by the builder.
3168+ /// The first parameter of the function is an i8 pointer points to the captured data.
31373169 fn create_closure_fn (
31383170 & self ,
31393171 ctx : & mut Ctx < ' a > ,
@@ -3160,6 +3192,7 @@ impl<'a, 'ctx> IRBuilder<'a, 'ctx> for LLVMBuilder<'a, 'ctx> {
31603192 f_v. set_call_conventions ( CALL_CONV ) ;
31613193 self . get_llvm_value_handle ( & f_v. into ( ) )
31623194 }
3195+
31633196 /// # get_closure_trampoline
31643197 ///
31653198 /// 为指定函数创建一个用于构建闭包的跳板函数
0 commit comments