v / vlib / v2 / gen / x64 / diagnostics.v
80 lines · 67 sloc · 3.29 KB · e78b7311ad580c800e5a3a0bd0afe3876e609685
Raw
1// Copyright (c) 2026 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4
5module x64
6
7const x64_backend_feature_prefix = 'backend feature: '
8pub const x64_backend_limitation_hint = 'x64: native backend limitation: retry with the C backend or reduce the source to supported x64 features'
9
10const x64_minimal_unsupported_crt_stdio_symbols = ['stdin', 'stdout', 'stderr', 'printf', 'fprintf',
11 'dprintf', 'sprintf', 'snprintf', 'wprintf', 'puts', 'fputs', 'setvbuf', 'fflush', 'fopen',
12 '_wfopen', 'fdopen', 'freopen', '_wfreopen', 'fclose', 'pclose', '_pclose', 'fread', 'fwrite',
13 'fgets', 'getc', 'feof', 'ferror', 'fseek', 'ftell', 'rewind', 'fileno', '_fileno',
14 '_get_osfhandle', '_open_osfhandle', 'fgetpos']
15
16const x64_unsupported_captured_fn_literal_symbol = 'v2_unsupported_captured_fn_literal'
17const x64_missing_v_runtime_helpers = ['builtin__Map_string_int__keys']
18
19fn x64_normalize_backend_feature(feature string) string {
20 if feature.starts_with(x64_backend_feature_prefix) {
21 return feature[x64_backend_feature_prefix.len..]
22 }
23 return feature
24}
25
26fn x64_unsupported_backend_feature_message(feature string) string {
27 return 'x64: unsupported backend feature: ${x64_normalize_backend_feature(feature)}'
28}
29
30fn x64_unsupported(message string) {
31 x64_abort_with_diagnostic(x64_unsupported_backend_feature_message(message))
32}
33
34fn x64_abort_with_diagnostic(message string) {
35 eprintln(message)
36 eprintln(x64_backend_limitation_hint)
37 exit(1)
38}
39
40fn x64_linker_name(format ObjectFormat) string {
41 return match format {
42 .elf { 'ELF linker' }
43 .macho { 'Mach-O linker' }
44 .coff { 'PE linker' }
45 }
46}
47
48fn x64_unresolved_external_symbol_message(format ObjectFormat, name string, context string) string {
49 linker_name := x64_linker_name(format)
50 if format == .coff && name in x64_minimal_unsupported_crt_stdio_symbols {
51 return x64_unsupported_backend_feature_message('${linker_name} cannot resolve C stdio/file-descriptor symbol `${name}`: Windows x64 native backend uses Kernel32 handles, not C FILE/stdio calls; ${context}')
52 }
53 if name == x64_unsupported_captured_fn_literal_symbol {
54 return x64_unsupported_backend_feature_message('native x64 cannot lower captured function literal: closure environments are not implemented yet; ${context}')
55 }
56 if name in x64_missing_v_runtime_helpers {
57 return x64_unsupported_backend_feature_message('${linker_name} cannot resolve V runtime helper `${name}`: native x64 backend does not implement this feature for this target yet; ${context}')
58 }
59 return x64_unsupported_backend_feature_message('${linker_name} cannot resolve external symbol `${name}` yet; ${context}')
60}
61
62pub fn unsupported_external_symbol_message_for_name(format ObjectFormat, raw_name string, context string) ?string {
63 name := x64_normalize_external_symbol_name(format, raw_name)
64 if x64_symbol_needs_backend_runtime_support(name) {
65 return x64_unresolved_external_symbol_message(format, name, context)
66 }
67 return none
68}
69
70fn x64_normalize_external_symbol_name(format ObjectFormat, name string) string {
71 if format == .macho && name.starts_with('_') {
72 return name[1..]
73 }
74 return name
75}
76
77fn x64_symbol_needs_backend_runtime_support(name string) bool {
78 return name == x64_unsupported_captured_fn_literal_symbol
79 || name in x64_missing_v_runtime_helpers
80}
81