From 152d35f8470310519ae0625c339c8a163f22d9f2 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 16 Nov 2024 10:44:41 -0300 Subject: [PATCH 1/2] cleanup --- vlib/v/markused/markused.v | 80 ++++++++------------------------------ 1 file changed, 16 insertions(+), 64 deletions(-) diff --git a/vlib/v/markused/markused.v b/vlib/v/markused/markused.v index df6bd2847ff8e8..38e06ddc5a494c 100644 --- a/vlib/v/markused/markused.v +++ b/vlib/v/markused/markused.v @@ -22,14 +22,12 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a } else { byteptr_idx_str := '${ast.byteptr_type_idx}' charptr_idx_str := '${ast.charptr_type_idx}' - u8_idx_str := '${ast.u8_type_idx}' string_idx_str := '${ast.string_type_idx}' array_idx_str := '${ast.array_type_idx}' map_idx_str := '${ast.map_type_idx}' ref_array_idx_str := '${int(ast.array_type.ref())}' mut core_fns := [ 'main.main', - //'format_sb', 'init_global_allocator', // needed for linux_bare and wasm_bare 'v_realloc', // needed for _STR 'malloc', @@ -40,55 +38,20 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a 'vstrlen', 'tos', 'tos2', - '_option_ok', - '_result_ok', 'error', 'builtin_init', - // byte. methods - u8_idx_str + '.str_escaped', - // string. methods - // string_idx_str + '.add', - // string_idx_str + '.trim_space', - // string_idx_str + '.replace', - // string_idx_str + '.trim', - // string_idx_str + '.index_kmp', - // string. ==, !=, etc... - // string_idx_str + '.eq', - // string_idx_str + '.ne', - // string_idx_str + '.lt', - // string_idx_str + '.gt', - // string_idx_str + '.le', - // string_idx_str + '.ge', 'fast_string_eq', - // other array methods - // array_idx_str + '.get_unsafe', - // array_idx_str + '.set_unsafe', - // array_idx_str + '.first', - // array_idx_str + '.last', - // array_idx_str + '.pointers', // TODO: handle generic methods calling array primitives more precisely in pool_test.v - // array_idx_str + '.reverse', - // array_idx_str + '.repeat_to_depth', - // reference array methods - // ref_array_idx_str + '.last', - // ref_array_idx_str + '.pop', - // ref_array_idx_str + '.insert_many', - // ref_array_idx_str + '.reverse', - // ref_array_idx_str + '.set_unsafe', // TODO: process the _vinit const initializations automatically too - // 'main.nasserts', 'main.vtest_init', 'main.vtest_new_metainfo', 'main.vtest_new_filemetainfo', - // 'os.getwd', - // 'v.embed_file.find_index_entry_by_path', ] $if debug_used_features ? { dump(table.used_features) } // real world apps if table.used_features.builtin_types || table.used_features.as_cast - || table.used_features.auto_str || table.used_features.option_or_result { - // println('used builtin') + || table.used_features.auto_str { core_fns << '__new_array' core_fns << '__new_array_with_default' core_fns << '__new_array_with_multi_default' @@ -151,35 +114,24 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a } else { // TODO: this *should not* depend on the used compiler, which is brittle, but only on info in the AST... // hello world apps - if pref_.ccompiler_type == .tinyc { - // unused on tcc - all_fns.delete(ref_array_idx_str + '.ensure_cap') - all_fns.delete(u8_idx_str + '.str_escaped') - all_fns.delete(u8_idx_str + '.ascii_str') - allow_noscan = false + if pref_.ccompiler_type != .tinyc && 'no_backtrace' !in pref_.compile_defines { + // with backtrace on gcc/clang more code needs be generated + allow_noscan = true + core_fns << '__new_array_with_default' + core_fns << 'str_intp' + core_fns << ref_array_idx_str + '.push' + core_fns << string_idx_str + '.substr' + core_fns << array_idx_str + '.slice' + core_fns << array_idx_str + '.get' + core_fns << 'v_fixed_index' } else { - if 'no_backtrace' in pref_.compile_defines { - // with -d no_backtrace on gcc/clang - allow_noscan = false - all_fns.delete(ref_array_idx_str + '.slice') - all_fns.delete(ref_array_idx_str + '.ensure_cap') - all_fns.delete(array_idx_str + '.get_unsafe') - all_fns.delete(u8_idx_str + '.str_escaped') - all_fns.delete(u8_idx_str + '.ascii_str') - } else { - // with backtrace on gcc/clang - core_fns << ref_array_idx_str + '.push_many_noscan' - core_fns << '__new_array_with_default' - core_fns << '__new_array_with_default_noscan' - core_fns << 'str_intp' - core_fns << ref_array_idx_str + '.push' - core_fns << string_idx_str + '.substr' - core_fns << array_idx_str + '.slice' - core_fns << array_idx_str + '.get' - core_fns << 'v_fixed_index' - } + allow_noscan = false } } + if table.used_features.option_or_result { + core_fns << '_option_ok' + core_fns << '_result_ok' + } if table.used_features.as_cast { core_fns << '__as_cast' } From 124f69fe08b7688fd6dbe51468e7bae5480bec93 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 16 Nov 2024 19:14:34 -0300 Subject: [PATCH 2/2] cleanup --- vlib/v/markused/markused.v | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/vlib/v/markused/markused.v b/vlib/v/markused/markused.v index 38e06ddc5a494c..754ce95e32dda5 100644 --- a/vlib/v/markused/markused.v +++ b/vlib/v/markused/markused.v @@ -49,15 +49,23 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a $if debug_used_features ? { dump(table.used_features) } + panic_deps := [ + '__new_array_with_default', + 'str_intp', + ref_array_idx_str + '.push', + string_idx_str + '.substr', + array_idx_str + '.slice', + array_idx_str + '.get', + 'v_fixed_index', + ] // real world apps if table.used_features.builtin_types || table.used_features.as_cast || table.used_features.auto_str { + core_fns << panic_deps core_fns << '__new_array' - core_fns << '__new_array_with_default' core_fns << '__new_array_with_multi_default' core_fns << '__new_array_with_array_default' core_fns << 'new_array_from_c_array' - core_fns << 'str_intp' // byteptr and charptr core_fns << byteptr_idx_str + '.vstring' core_fns << byteptr_idx_str + '.vstring_with_len' @@ -67,15 +75,11 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a core_fns << charptr_idx_str + '.vstring_literal' if table.used_features.index { - core_fns << 'v_fixed_index' core_fns << string_idx_str + '.at_with_check' core_fns << string_idx_str + '.clone' core_fns << string_idx_str + '.clone_static' core_fns << string_idx_str + '.at' - core_fns << array_idx_str + '.slice' - core_fns << array_idx_str + '.get' core_fns << array_idx_str + '.set' - core_fns << ref_array_idx_str + '.push' core_fns << ref_array_idx_str + '.set' core_fns << map_idx_str + '.get' core_fns << map_idx_str + '.set' @@ -87,7 +91,6 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a core_fns << array_idx_str + '.get_with_check' // used for `x := a[i] or {}` core_fns << array_idx_str + '.clone_static_to_depth' core_fns << array_idx_str + '.clone_to_depth' - core_fns << string_idx_str + '.substr' } if table.used_features.cast_ptr { core_fns << 'ptr_str' // TODO: remove this. It is currently needed for the auto str methods for &u8, fn types, etc; See `./v -skip-unused vlib/builtin/int_test.v` @@ -117,13 +120,7 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a if pref_.ccompiler_type != .tinyc && 'no_backtrace' !in pref_.compile_defines { // with backtrace on gcc/clang more code needs be generated allow_noscan = true - core_fns << '__new_array_with_default' - core_fns << 'str_intp' - core_fns << ref_array_idx_str + '.push' - core_fns << string_idx_str + '.substr' - core_fns << array_idx_str + '.slice' - core_fns << array_idx_str + '.get' - core_fns << 'v_fixed_index' + core_fns << panic_deps } else { allow_noscan = false } @@ -131,6 +128,10 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a if table.used_features.option_or_result { core_fns << '_option_ok' core_fns << '_result_ok' + if !allow_noscan { + core_fns << panic_deps + allow_noscan = true + } } if table.used_features.as_cast { core_fns << '__as_cast'