Skip to content

Commit 2e8cc75

Browse files
authored
markused: cleanup the generated c code (#25210)
1 parent 840e659 commit 2e8cc75

11 files changed

Lines changed: 62 additions & 28 deletions

File tree

‎cmd/tools/vast/vast.v‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ fn (t Tree) fn_decl(node ast.FnDecl) &Node {
596596
obj.add_terse('is_variadic', t.bool_node(node.is_variadic))
597597
obj.add('is_anon', t.bool_node(node.is_anon))
598598
obj.add_terse('is_noreturn', t.bool_node(node.is_noreturn))
599+
obj.add_terse('is_weak', t.bool_node(node.is_weak))
599600
obj.add_terse('is_manualfree', t.bool_node(node.is_manualfree))
600601
obj.add('is_main', t.bool_node(node.is_main))
601602
obj.add('is_test', t.bool_node(node.is_test))
@@ -840,6 +841,8 @@ fn (t Tree) global_field(node ast.GlobalField) &Node {
840841
obj.add_terse('typ', t.type_node(node.typ))
841842
obj.add_terse('has_expr', t.bool_node(node.has_expr))
842843
obj.add_terse('is_markused', t.bool_node(node.is_markused))
844+
obj.add_terse('is_weak', t.bool_node(node.is_weak))
845+
obj.add_terse('is_hidden', t.bool_node(node.is_hidden))
843846
obj.add('comments', t.array_node_comment(node.comments))
844847
obj.add('pos', t.pos(node.pos))
845848
obj.add('typ_pos', t.pos(node.typ_pos))

‎vlib/v/ast/ast.v‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ pub:
446446
module_pos int = -1 // module:
447447
is_union bool
448448
is_option bool
449+
is_aligned bool
449450
attrs []Attr
450451
pre_comments []Comment
451452
end_comments []Comment
@@ -593,6 +594,7 @@ pub:
593594
is_c_extern bool
594595
is_variadic bool
595596
is_anon bool
597+
is_weak bool
596598
is_noreturn bool // true, when @[noreturn] is used on a fn
597599
is_manualfree bool // true, when @[manualfree] is used on a fn
598600
is_main bool // true for `fn main()`
@@ -967,6 +969,8 @@ pub:
967969
is_markused bool // an explicit `@[markused]` tag; the global will NOT be removed by `-skip-unused`
968970
is_volatile bool
969971
is_exported bool // an explicit `@[export]` tag; the global will NOT be removed by `-skip-unused`
972+
is_weak bool
973+
is_hidden bool
970974
pub mut:
971975
expr Expr
972976
typ Type

‎vlib/v/ast/table.v‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ pub mut:
3838
// json bool // json is imported
3939
comptime_calls map[string]bool // resolved name to call on comptime
4040
comptime_syms map[Type]bool // resolved syms (generic)
41+
//
42+
used_attr_noreturn bool // @[noreturn]
43+
used_attr_hidden bool // @[hidden]
44+
used_attr_weak bool // @[weak]
4145
}
4246

4347
@[unsafe]

‎vlib/v/gen/c/cgen.v‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,16 @@ pub fn (mut g Gen) init() {
10011001
} else {
10021002
g.cheaders.writeln(c_headers)
10031003
}
1004+
if !g.pref.skip_unused || g.table.used_features.used_attr_weak {
1005+
g.cheaders.writeln(c_common_weak_attr)
1006+
}
1007+
if !g.pref.skip_unused || g.table.used_features.used_attr_hidden {
1008+
g.cheaders.writeln(c_common_hidden_attr)
1009+
}
1010+
if !g.pref.skip_unused || g.table.used_features.used_attr_noreturn {
1011+
g.cheaders.writeln(c_common_noreturn_attr)
1012+
g.cheaders.writeln(c_common_unreachable_attr)
1013+
}
10041014
if !g.pref.skip_unused || g.table.used_features.used_maps > 0 {
10051015
g.cheaders.writeln(c_wyhash_headers)
10061016
}

‎vlib/v/gen/c/cheaders.v‎

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@ const c_common_macros = '
180180
#endif
181181
#endif
182182
183-
#define OPTION_CAST(x) (x)
184-
185183
#if defined(_WIN32) || defined(__CYGWIN__)
186184
#define VV_EXP extern __declspec(dllexport)
187185
#define VV_LOC static
@@ -220,7 +218,17 @@ const c_common_macros = '
220218
#undef __has_include
221219
#endif
222220
221+
//likely and unlikely macros
222+
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
223+
#define _likely_(x) __builtin_expect(x,1)
224+
#define _unlikely_(x) __builtin_expect(x,0)
225+
#else
226+
#define _likely_(x) (x)
227+
#define _unlikely_(x) (x)
228+
#endif
229+
'
223230

231+
const c_common_weak_attr = '
224232
#if !defined(VWEAK)
225233
#define VWEAK __attribute__((weak))
226234
#ifdef _MSC_VER
@@ -232,7 +240,9 @@ const c_common_macros = '
232240
#define VWEAK
233241
#endif
234242
#endif
243+
'
235244

245+
const c_common_hidden_attr = '
236246
#if !defined(VHIDDEN)
237247
#define VHIDDEN __attribute__((visibility("hidden")))
238248
#ifdef _MSC_VER
@@ -244,7 +254,9 @@ const c_common_macros = '
244254
#define VHIDDEN
245255
#endif
246256
#endif
257+
'
247258

259+
const c_common_noreturn_attr = '
248260
#if !defined(VNORETURN)
249261
#if defined(__TINYC__)
250262
#include <stdnoreturn.h>
@@ -259,7 +271,9 @@ const c_common_macros = '
259271
#define VNORETURN
260272
#endif
261273
#endif
274+
'
262275

276+
const c_common_unreachable_attr = '
263277
#if !defined(VUNREACHABLE)
264278
#if defined(__GNUC__) && !defined(__clang__)
265279
#define V_GCC_VERSION (__GNUC__ * 10000L + __GNUC_MINOR__ * 100L + __GNUC_PATCHLEVEL__)
@@ -276,16 +290,6 @@ const c_common_macros = '
276290
#define VUNREACHABLE() do { } while (0)
277291
#endif
278292
#endif
279-
280-
//likely and unlikely macros
281-
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
282-
#define _likely_(x) __builtin_expect(x,1)
283-
#define _unlikely_(x) __builtin_expect(x,0)
284-
#else
285-
#define _likely_(x) (x)
286-
#define _unlikely_(x) (x)
287-
#endif
288-
289293
'
290294

291295
const c_unsigned_comparison_functions = '
@@ -433,8 +437,6 @@ void v_free(voidptr ptr);
433437
#define _Atomic volatile
434438
435439
// MSVC cannot parse some things properly
436-
#undef OPTION_CAST
437-
#define OPTION_CAST(x)
438440
#undef __NOINLINE
439441
#undef __IRQHANDLER
440442
#define __NOINLINE __declspec(noinline)
@@ -451,9 +453,6 @@ void v_free(voidptr ptr);
451453
#endif
452454
#endif
453455
454-
// g_live_info is used by live.info()
455-
static void* g_live_info = NULL;
456-
457456
#if defined(__MINGW32__) || defined(__MINGW64__) || (defined(_WIN32) && defined(__TINYC__))
458457
#undef PRId64
459458
#undef PRIi64
@@ -711,5 +710,4 @@ static inline uint64_t wy2u0k(uint64_t r, uint64_t k){ _wymum(&r,&k); return k;
711710
#endif
712711
713712
#define _IN_MAP(val, m) map_exists(m, val)
714-
715713
'

‎vlib/v/gen/c/consts_and_globals.v‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,14 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) {
437437
should_init := (!g.pref.use_cache && g.pref.build_mode != .build_module)
438438
|| (g.pref.build_mode == .build_module && g.module_built == node.mod)
439439
mut attributes := ''
440-
if node.attrs.contains('weak') {
440+
first_field := node.fields[0]
441+
if first_field.is_weak {
441442
attributes += 'VWEAK '
442443
}
443-
if node.attrs.contains('hidden') {
444+
if first_field.is_hidden {
444445
attributes += 'VHIDDEN '
445446
}
446-
if node.attrs.contains('export') {
447+
if first_field.is_exported {
447448
attributes += 'VV_EXP '
448449
}
449450
if attr := node.attrs.find_first('_linker_section') {

‎vlib/v/gen/c/fn.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,9 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
532532
if g.pref.printfn_list.len > 0 && g.last_fn_c_name in g.pref.printfn_list {
533533
println(g.out.after(fn_start_pos))
534534
}
535+
weak := if node.is_weak { 'VWEAK ' } else { '' }
535536
for attr in node.attrs {
536537
if attr.name == 'export' {
537-
weak := if node.attrs.any(it.name == 'weak') { 'VWEAK ' } else { '' }
538538
g.writeln('// export alias: ${attr.arg} -> ${name}')
539539
g.export_funcs << attr.arg
540540
export_alias := '${weak}${type_name} ${fn_attrs}${attr.arg}(${arg_str})'

‎vlib/v/markused/walker.v‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ pub fn (mut w Walker) mark_global_as_used(ckey string) {
142142
}
143143
w.used_globals[ckey] = true
144144
gfield := w.all_globals[ckey] or { return }
145+
w.table.used_features.used_attr_weak = w.table.used_features.used_attr_weak || gfield.is_weak
146+
w.table.used_features.used_attr_hidden = w.table.used_features.used_attr_hidden
147+
|| gfield.is_hidden || gfield.is_hidden
145148
w.expr(gfield.expr)
146149
if !gfield.has_expr {
147150
w.mark_by_type(gfield.typ)
@@ -355,9 +358,7 @@ pub fn (mut w Walker) stmt(node_ ast.Stmt) {
355358
w.mark_by_type(typ.typ)
356359
}
357360
w.struct_fields(node.fields)
358-
if !w.uses_mem_align {
359-
w.uses_mem_align = node.attrs.contains('aligned')
360-
}
361+
w.uses_mem_align = w.uses_mem_align || node.is_aligned
361362
}
362363
ast.DeferStmt {
363364
w.stmts(node.stmts)
@@ -894,6 +895,9 @@ pub fn (mut w Walker) fn_decl(mut node ast.FnDecl) {
894895
w.is_builtin_mod = last_is_builtin_mod
895896
}
896897
}
898+
w.table.used_features.used_attr_weak = w.table.used_features.used_attr_weak || node.is_weak
899+
w.table.used_features.used_attr_noreturn = w.table.used_features.used_attr_noreturn
900+
|| node.is_noreturn
897901
if node.language == .c {
898902
w.mark_fn_as_used(node.fkey())
899903
w.mark_fn_ret_and_params(node.return_type, node.params)
@@ -1153,9 +1157,7 @@ pub fn (mut w Walker) mark_by_sym(isym ast.TypeSymbol) {
11531157
}
11541158
if decl := w.all_structs[isym.name] {
11551159
w.struct_fields(decl.fields)
1156-
if !w.uses_mem_align {
1157-
w.uses_mem_align = decl.attrs.contains('aligned')
1158-
}
1160+
w.uses_mem_align = w.uses_mem_align || decl.is_aligned
11591161
for iface_typ in decl.implements_types {
11601162
w.mark_by_type(iface_typ.typ)
11611163
}

‎vlib/v/parser/fn.v‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
213213
mut is_c2v_variadic := false
214214
mut is_c_extern := false
215215
mut is_markused := false
216+
mut is_weak := false
216217
mut is_expand_simple_interpolation := false
217218
mut comments := []ast.Comment{}
218219
fn_attrs := p.attrs
@@ -254,6 +255,9 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
254255
'c2v_variadic' {
255256
is_c2v_variadic = true
256257
}
258+
'weak' {
259+
is_weak = true
260+
}
257261
'use_new' {
258262
is_ctor_new = true
259263
}
@@ -706,6 +710,7 @@ run them via `v file.v` instead',
706710
is_unsafe: is_unsafe
707711
is_must_use: is_must_use
708712
is_markused: is_markused
713+
is_weak: is_weak
709714
is_file_translated: p.is_translated
710715
//
711716
attrs: fn_attrs

‎vlib/v/parser/parser.v‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,10 +2599,14 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
25992599

26002600
mut is_markused := false
26012601
mut is_exported := false
2602+
mut is_weak := false
2603+
mut is_hidden := false
26022604
for ga in attrs {
26032605
match ga.name {
26042606
'export' { is_exported = true }
26052607
'markused' { is_markused = true }
2608+
'weak' { is_weak = true }
2609+
'hidden' { is_hidden = true }
26062610
else {}
26072611
}
26082612
}
@@ -2687,6 +2691,8 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
26872691
is_markused: is_markused
26882692
is_volatile: is_volatile
26892693
is_exported: is_exported
2694+
is_weak: is_weak
2695+
is_hidden: is_hidden
26902696
}
26912697
fields << field
26922698
if name !in ast.global_reserved_type_names {

0 commit comments

Comments
 (0)