| ... | ... | @@ -2582,9 +2582,28 @@ static void gen_const_globals(CodeGen *g) { |
| 2582 | 2582 | } |
| 2583 | 2583 | } |
| 2584 | 2584 | |
| 2585 | static void delete_unused_builtin_fns(CodeGen *g) { |
| 2586 | auto it = g->builtin_fn_table.entry_iterator(); |
| 2587 | for (;;) { |
| 2588 | auto *entry = it.next(); |
| 2589 | if (!entry) |
| 2590 | break; |
| 2591 | |
| 2592 | BuiltinFnEntry *builtin_fn = entry->value; |
| 2593 | if (builtin_fn->ref_count == 0 && |
| 2594 | builtin_fn->fn_val) |
| 2595 | { |
| 2596 | LLVMDeleteFunction(entry->value->fn_val); |
| 2597 | } |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2585 | 2601 | static void do_code_gen(CodeGen *g) { |
| 2586 | 2602 | assert(!g->errors.length); |
| 2587 | 2603 | |
| 2604 | delete_unused_builtin_fns(g); |
| 2605 | |
| 2606 | |
| 2588 | 2607 | gen_const_globals(g); |
| 2589 | 2608 | |
| 2590 | 2609 | // Generate module level variables |
| ... | ... | @@ -2633,6 +2652,12 @@ static void do_code_gen(CodeGen *g) { |
| 2633 | 2652 | // Generate function prototypes |
| 2634 | 2653 | for (int fn_proto_i = 0; fn_proto_i < g->fn_protos.length; fn_proto_i += 1) { |
| 2635 | 2654 | FnTableEntry *fn_table_entry = g->fn_protos.at(fn_proto_i); |
| 2655 | if (fn_table_entry->ref_count == 0) { |
| 2656 | // huge time saver |
| 2657 | LLVMDeleteFunction(fn_table_entry->fn_value); |
| 2658 | continue; |
| 2659 | } |
| 2660 | |
| 2636 | 2661 | AstNode *proto_node = fn_table_entry->proto_node; |
| 2637 | 2662 | assert(proto_node->type == NodeTypeFnProto); |
| 2638 | 2663 | AstNodeFnProto *fn_proto = &proto_node->data.fn_proto; |
| ... | ... | @@ -2681,6 +2706,11 @@ static void do_code_gen(CodeGen *g) { |
| 2681 | 2706 | // Generate function definitions. |
| 2682 | 2707 | for (int fn_i = 0; fn_i < g->fn_defs.length; fn_i += 1) { |
| 2683 | 2708 | FnTableEntry *fn_table_entry = g->fn_defs.at(fn_i); |
| 2709 | if (fn_table_entry->ref_count == 0) { |
| 2710 | // huge time saver |
| 2711 | continue; |
| 2712 | } |
| 2713 | |
| 2684 | 2714 | ImportTableEntry *import = fn_table_entry->import_entry; |
| 2685 | 2715 | AstNode *fn_def_node = fn_table_entry->fn_def_node; |
| 2686 | 2716 | LLVMValueRef fn = fn_table_entry->fn_value; |
| ... | ... | @@ -3064,6 +3094,7 @@ static void define_builtin_fns(CodeGen *g) { |
| 3064 | 3094 | builtin_fn->param_types[0] = nullptr; // manually checked later |
| 3065 | 3095 | builtin_fn->param_types[1] = nullptr; // manually checked later |
| 3066 | 3096 | builtin_fn->param_types[2] = g->builtin_types.entry_isize; |
| 3097 | builtin_fn->ref_count = 1; |
| 3067 | 3098 | |
| 3068 | 3099 | LLVMTypeRef param_types[] = { |
| 3069 | 3100 | LLVMPointerType(LLVMInt8Type(), 0), |
| ... | ... | @@ -3087,6 +3118,7 @@ static void define_builtin_fns(CodeGen *g) { |
| 3087 | 3118 | builtin_fn->param_types[0] = nullptr; // manually checked later |
| 3088 | 3119 | builtin_fn->param_types[1] = g->builtin_types.entry_u8; |
| 3089 | 3120 | builtin_fn->param_types[2] = g->builtin_types.entry_isize; |
| 3121 | builtin_fn->ref_count = 1; |
| 3090 | 3122 | |
| 3091 | 3123 | LLVMTypeRef param_types[] = { |
| 3092 | 3124 | LLVMPointerType(LLVMInt8Type(), 0), |