cgen: improve support for v -path bootstrap_alternative_backend/ run simple.v (#23679)

This commit is contained in:
Delyan Angelov 2025-02-09 21:23:14 +02:00 committed by GitHub
parent 089778e55e
commit adbc869626
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 12 deletions

View File

@ -158,7 +158,3 @@ fn __exit(code int) {
fn __qsort(base voidptr, nmemb usize, size usize, sort_cb FnSortCB) {
panic('qsort() is not yet implemented in `-freestanding`')
}
fn init_global_allocator() {
// global_allocator = dlmalloc.new(get_wasm_allocator())
}

View File

@ -116,7 +116,9 @@ fn (mut g Gen) gen_assert_postfailure_mode(node ast.AssertStmt) {
}
if g.pref.assert_failure_mode == .backtraces
|| g.fn_decl.attrs.any(it.name == 'assert_backtraces') {
g.writeln('\tprint_backtrace();')
if _ := g.table.fns['print_backtrace'] {
g.writeln('\tprint_backtrace();')
}
}
if g.pref.is_test {
g.writeln('\tlongjmp(g_jump_buffer, 1);')

View File

@ -6213,17 +6213,25 @@ fn (mut g Gen) write_init_function() {
}
if g.use_segfault_handler && !g.pref.is_shared {
// 11 is SIGSEGV. It is hardcoded here, to avoid FreeBSD compilation errors for trivial examples.
// shared object does not need this
g.writeln('#if __STDC_HOSTED__ == 1\n\tsignal(11, v_segmentation_fault_handler);\n#endif')
if _ := g.table.fns['v_segmentation_fault_handler'] {
// 11 is SIGSEGV. It is hardcoded here, to avoid FreeBSD compilation errors for trivial examples.
// shared object does not need this
g.writeln('#if __STDC_HOSTED__ == 1\n\tsignal(11, v_segmentation_fault_handler);\n#endif')
}
}
if g.pref.is_bare {
g.writeln('init_global_allocator();')
if _ := g.table.fns['init_global_allocator'] {
g.writeln('init_global_allocator();')
}
}
if g.pref.prealloc {
g.writeln('prealloc_vinit();')
if _ := g.table.fns['prealloc_vinit'] {
g.writeln('prealloc_vinit();')
}
}
// Note: the as_cast table should be *before* the other constant initialize calls,
// because it may be needed during const initialization of builtin and during
// calling module init functions too, just in case they do fail...
@ -6233,7 +6241,9 @@ fn (mut g Gen) write_init_function() {
}
if !g.pref.is_shared && (!g.pref.skip_unused || g.table.used_features.external_types) {
// shared object does not need this
g.writeln('\tbuiltin_init();')
if _ := g.table.find_fn('builtin_init') {
g.writeln('\tbuiltin_init();')
}
}
if g.nr_closures > 0 {

View File

@ -135,7 +135,12 @@ fn (mut g Gen) gen_c_main_function_header() {
g.gen_c_main_function_only_header()
g.gen_c_main_trace_calls_hook()
if !g.pref.no_builtin {
g.writeln2('\tg_main_argc = ___argc;', '\tg_main_argv = ___argv;')
if _ := g.table.global_scope.find_global('g_main_argc') {
g.writeln('\tg_main_argc = ___argc;')
}
if _ := g.table.global_scope.find_global('g_main_argv') {
g.writeln('\tg_main_argv = ___argv;')
}
}
if g.nr_closures > 0 {
g.writeln('\t__closure_init(); // main()')