Skip to content

Commit

Permalink
cgen: fix compilation for a project using a lot of json, threads, emb…
Browse files Browse the repository at this point in the history
…eds + parallel-cc (#23467)
  • Loading branch information
felipensp authored Jan 14, 2025
1 parent dacdafb commit 9ba294b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
35 changes: 22 additions & 13 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,6 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO
if g.out_results.len > 0 {
b.write_string2('\n// V result_xxx definitions:\n', g.out_results.str())
}
if g.json_forward_decls.len > 0 {
b.write_string2('\n// V json forward decls:\n', g.json_forward_decls.str())
}
b.write_string2('\n// V definitions:\n', g.definitions.str())
if g.sort_fn_definitions.len > 0 {
b.write_string2('\n// V sort fn definitions:\n', g.sort_fn_definitions.str())
Expand All @@ -621,18 +618,9 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO
if interface_table.len > 0 {
b.write_string2('\n// V interface table:\n', interface_table)
}
if g.waiter_fn_definitions.len > 0 {
b.write_string2('\n// V gowrappers waiter fns:\n', g.waiter_fn_definitions.str())
}
if g.gowrappers.len > 0 {
b.write_string2('\n// V gowrappers:\n', g.gowrappers.str())
}
if g.hotcode_definitions.len > 0 {
b.write_string2('\n// V hotcode definitions:\n', g.hotcode_definitions.str())
}
if g.embedded_data.len > 0 {
b.write_string2('\n// V embedded data:\n', g.embedded_data.str())
}
if g.shared_functions.len > 0 {
b.writeln('\n// V shared type functions:\n')
b.write_string2(g.shared_functions.str(), c_concurrency_helpers)
Expand All @@ -659,10 +647,13 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO
header = '#ifndef V_HEADER_FILE\n#define V_HEADER_FILE' + header
header += '\n#endif\n'

mut helpers := strings.new_builder(200_000)
mut helpers := strings.new_builder(300_000)
// Code added here (after the header) goes to out_0.c in parallel cc mode
// Previously it went to the header which resulted in duplicated code and more code
// to compile for the C compiler
if g.embedded_data.len > 0 {
helpers.write_string2('\n// V embedded data:\n', g.embedded_data.str())
}
if g.anon_fn_definitions.len > 0 {
if g.nr_closures > 0 {
helpers.writeln2('\n// V closure helpers', c_closure_fn_helpers(g.pref))
Expand Down Expand Up @@ -693,6 +684,12 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO
}
}
}
if g.waiter_fn_definitions.len > 0 {
if g.pref.parallel_cc {
g.extern_out.write_string2('\n// V gowrappers waiter fns:\n', g.waiter_fn_definitions.bytestr())
}
helpers.write_string2('\n// V gowrappers waiter fns:\n', g.waiter_fn_definitions.str())
}
if g.auto_str_funcs.len > 0 {
helpers.write_string2('\n// V auto str functions:\n', g.auto_str_funcs.str())
}
Expand All @@ -702,6 +699,15 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO
helpers.writeln(fn_def)
}
}
if g.json_forward_decls.len > 0 {
helpers.write_string2('\n// V json forward decls:\n', g.json_forward_decls.bytestr())
if g.pref.parallel_cc {
g.extern_out.write_string2('\n// V json forward decls:\n', g.json_forward_decls.str())
}
}
if g.gowrappers.len > 0 {
helpers.write_string2('\n// V gowrappers:\n', g.gowrappers.str())
}
if g.dump_funcs.len > 0 {
helpers.write_string2('\n// V dump functions:\n', g.dump_funcs.str())
}
Expand Down Expand Up @@ -1152,6 +1158,9 @@ pub fn (mut g Gen) write_typeof_functions() {
}
g.writeln2('\treturn "unknown ${util.strip_main_name(sym.name)}";', '}')
g.writeln2('', 'int v_typeof_interface_idx_${sym.cname}(int sidx) {')
if g.pref.parallel_cc {
g.extern_out.writeln('extern int v_typeof_interface_idx_${sym.cname}(int sidx);')
}
for t in inter_info.types {
sub_sym := g.table.sym(ast.mktyp(t))
if sub_sym.info is ast.Struct && sub_sym.info.is_unresolved_generic() {
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/gen/c/embed.v
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ fn (mut g Gen) gen_embed_file_init(mut node ast.ComptimeCall) {
// into a single generated function _v_embed_file_metadata, that accepts a hash of the absolute path of the embedded
// files.
fn (mut g Gen) gen_embedded_metadata() {
if g.pref.parallel_cc {
g.extern_out.writeln('extern v__embed_file__EmbedFileData _v_embed_file_metadata(u64 ef_hash);')
}
g.embedded_data.writeln('v__embed_file__EmbedFileData _v_embed_file_metadata(u64 ef_hash) {')
g.embedded_data.writeln('\tv__embed_file__EmbedFileData res;')
g.embedded_data.writeln('\tmemset(&res, 0, sizeof(res));')
Expand Down
5 changes: 3 additions & 2 deletions vlib/v/gen/c/json.v
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,13 @@ ${dec_fn_dec} {
}
}
')
g.json_forward_decls.writeln('${dec_fn_dec};')
extern_str := if g.pref.parallel_cc { 'extern ' } else { '' }
g.json_forward_decls.writeln('${extern_str}${dec_fn_dec};')
// Codegen encoder
// encode_TYPE funcs receive an object to encode
enc_fn_name := js_enc_name(styp)
enc_fn_dec := 'cJSON* ${enc_fn_name}(${styp} val)'
g.json_forward_decls.writeln('${enc_fn_dec};\n')
g.json_forward_decls.writeln('${extern_str}${enc_fn_dec};\n')
enc.writeln('
${enc_fn_dec} {
\tcJSON *o;')
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/spawn_and_go.v
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ fn (mut g Gen) spawn_and_go_expr(node ast.SpawnExpr, mode SpawnGoMode) {
}
g.type_definitions.writeln('} ${wrapper_struct_name};')
thread_ret_type := if g.pref.os == .windows { 'u32' } else { 'void*' }
g.type_definitions.writeln('${g.static_modifier} ${thread_ret_type} ${wrapper_fn_name}(${wrapper_struct_name} *arg);')
g.waiter_fn_definitions.writeln('${g.static_non_parallel}${thread_ret_type} ${wrapper_fn_name}(${wrapper_struct_name} *arg);')
g.gowrappers.writeln('${thread_ret_type} ${wrapper_fn_name}(${wrapper_struct_name} *arg) {')
if call_ret_type != ast.void_type {
if g.pref.os == .windows {
Expand Down

0 comments on commit 9ba294b

Please sign in to comment.