Skip to content

Commit b21338c

Browse files
committed
v3: add full fastc backend
1 parent 2798aa9 commit b21338c

26 files changed

Lines changed: 64865 additions & 113 deletions

cmd/v/macos_v3_args.c.v

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn macos_v3_non_compilation_command(command string) bool {
2424
// precedence, options/modes V3 cannot honor yet, and whether the command is an
2525
// actual compilation command (never `test` or external tools). Compiler bootstrap
2626
// targets normally stay on V1, but explicit `-b fastc` owns those targets too: its
27-
// direct emitter falls back to V3's checked C backend for the full compiler source.
27+
// complete lane uses V3's checked frontend and full fastc generator for the compiler source.
2828
// Both the Darwin dispatcher (where it overrides the default heuristic) and the
2929
// non-macOS dispatcher (where it is the sole gate) rely on it, so it must stay
3030
// platform neutral.
@@ -36,7 +36,9 @@ fn macos_v3_force_requested(command string, prefs &pref.Preferences) bool {
3636
if v3_has_v1_only_preferences(prefs) || (prefs.gc_set_by_flag && prefs.gc_mode != .no_gc) {
3737
return false
3838
}
39-
if prefs.autofree && prefs.is_run {
39+
if prefs.autofree && prefs.is_run && !macos_v3_fastc_requested(prefs) {
40+
// V1 still owns the established `v -autofree run ...` orchestration.
41+
// FastC promotes autofree programs to its checked C lane.
4042
return false
4143
}
4244
if prefs.path == '' || command == 'test' || macos_v3_non_compilation_command(command)
@@ -65,6 +67,16 @@ fn macos_v3_fastc_requested(prefs &pref.Preferences) bool {
6567
return selected_backend == 'fastc'
6668
}
6769

70+
// macos_v3_test_ownership_uses_v1 keeps ownership/autofree test binaries on
71+
// V1. vtest marks its per-file compilations with `-skip-running`; compiling an
72+
// autofree test through the ownership-enabled V3 tool can consume far more
73+
// memory than the test itself. Direct V3 ownership builds remain available;
74+
// fastc uses the same checked ownership lane as the C backend.
75+
fn macos_v3_test_ownership_uses_v1(prefs &pref.Preferences, args []string) bool {
76+
return prefs.skip_running && !macos_v3_fastc_requested(prefs)
77+
&& (prefs.autofree || '-ownership' in args)
78+
}
79+
6880
// These helpers are shared by the native Darwin dispatcher and the default
6981
// implementation selected while generating cross-platform VC sources, so this
7082
// file has to stay platform neutral (no `_darwin.c.v` suffix). Keep them outside

cmd/v/macos_v3_darwin.c.v

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ fn maybe_delegate_to_macos_v3(command string, prefs &pref.Preferences) ?MacosV3C
3939
}
4040
all_args := util.join_env_vflags_and_os_args()
4141
forwarded_args := all_args[1..]
42+
if macos_v3_test_ownership_uses_v1(prefs, forwarded_args) {
43+
trace_macos_v3_skip('vtest ownership/autofree compilation')
44+
return take_macos_v3_c_error_report()
45+
}
4246
if !macos_v3_executable_can_dispatch(os.executable(), prefs) {
4347
trace_macos_v3_skip('non-default compiler executable `${os.executable()}`')
4448
return none
@@ -80,9 +84,10 @@ fn is_macos_v3_relevant_command(command string, prefs &pref.Preferences) bool {
8084
// dispatch, but it must not prevent V3 from being the default compiler.
8185
return false
8286
}
83-
if prefs.autofree && prefs.is_run {
87+
if prefs.autofree && prefs.is_run && !macos_v3_fastc_requested(prefs) {
8488
// V1 still owns the established `v -autofree run ...` orchestration.
8589
// Direct autofree builds are selected earlier by the ownership dispatcher.
90+
// FastC promotes autofree programs to its checked C lane.
8691
return false
8792
}
8893
if command == 'test' {

cmd/v/macos_v3_test.v

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,4 +1711,24 @@ fn test_macos_v3_fastc_routes_compiler_selfhost_targets() {
17111711
assert !macos_v3_fastc_requested(&pref.Preferences{
17121712
build_options: ['-b fastc', '-b c']
17131713
})
1714+
assert macos_v3_force_requested('run', &pref.Preferences{
1715+
new_compiler: true
1716+
autofree: true
1717+
is_run: true
1718+
path: 'main.v'
1719+
build_options: ['-b fastc']
1720+
})
1721+
}
1722+
1723+
fn test_macos_v3_vtest_ownership_modes_use_v1_except_fastc() {
1724+
mut prefs := &pref.Preferences{
1725+
skip_running: true
1726+
autofree: true
1727+
}
1728+
assert macos_v3_test_ownership_uses_v1(prefs, ['-skip-running', '-autofree', 'main.v'])
1729+
prefs.autofree = false
1730+
assert macos_v3_test_ownership_uses_v1(prefs, ['-skip-running', '-ownership', 'main.v'])
1731+
prefs.build_options = ['-b fastc']
1732+
assert !macos_v3_test_ownership_uses_v1(prefs, ['-skip-running', '-ownership', '-b',
1733+
'fastc', 'main.v'])
17141734
}

cmd/v/v.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ fn invoke_help_and_exit(remaining []string) {
219219
fn maybe_delegate_to_ownership(command string, prefs &pref.Preferences, merged_args []string) {
220220
is_ownership := '-ownership' in merged_args
221221
is_autofree := prefs.autofree
222+
$if macos {
223+
if macos_v3_test_ownership_uses_v1(prefs, merged_args) {
224+
return
225+
}
226+
}
222227
if !ownership_delegation_is_requested(is_ownership, is_autofree, prefs.old_compiler,
223228
os.user_os()) {
224229
return

vlib/v3/README.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,28 @@ edit-run cycle. It scans the source once and emits GNU C while consuming tokens.
9595
not create a flat AST and does not run imports, type checking, transform, type annotation, or
9696
mark-used. Bundled TinyCC compiles the emitted translation unit immediately.
9797

98-
FastC currently emits primitive functions and parameters, inferred local declarations, ordinary
99-
expressions, `if`/`else`, and condition, C-style, infinite, and integer-range `for` loops. GNU
100-
`typeof` carries `:=` declarations into C without V type inference. Unsupported syntax silently
101-
selects the normal C backend. A TinyCC error is also discarded and the original V source is
102-
reparsed by the normal C backend, so the user receives V parser or type-checker diagnostics rather
103-
than a speculative C diagnostic. A successfully compiled `run` program keeps its exit status and
104-
is never retried.
98+
FastC's direct lane currently emits primitive functions and parameters, inferred local
99+
declarations, ordinary expressions, `if`/`else`, and condition, C-style, infinite, and
100+
integer-range `for` loops. GNU
101+
`typeof` carries `:=` declarations into C without V type inference. Unsupported syntax promotes
102+
the source to fastc's complete lane, which uses the parser, checker, transformer, and mark-used
103+
pass, then emits C with its own `v3.gen.fastc.FlatGen` backend. That backend is a full fork of the
104+
V3 C generator rather than an alias or a runtime switch to `v3.gen.c`. A TinyCC error is also
105+
discarded and the original V source is compiled by the checked fastc lane, so the user receives V
106+
parser or type-checker diagnostics rather than a speculative C diagnostic. The complete lane has
107+
the same language and ownership/autofree coverage as the C backend. A successfully compiled `run`
108+
program keeps its exit status and is never retried.
105109

106110
The direct path is limited to host-target, non-production, non-test, non-shared single-file builds.
107-
Other modes select the normal C backend before source scanning. `-o file.c` emits the standalone
108-
fast C translation unit without invoking TinyCC.
111+
Compiler/self-host and other non-direct modes enter the complete lane before source scanning.
112+
`-o file.c` emits the standalone fast C translation unit when the direct lane supports the input;
113+
otherwise it emits the complete `v3.gen.fastc` translation unit.
109114

110115
`v self -b fastc` and direct compiler builds such as `v -b fastc -o v2 cmd/v` are routed to V3.
111-
The compiler source is outside the direct subset, so fastc immediately selects the checked V3 C
112-
backend for that build. The resulting self-hosted compiler retains fastc and can use the direct
113-
path for supported user programs, including when its output has a custom filename in the V checkout.
114-
The fastc integration test self-hosts the standalone V3 compiler through five successive generations
115-
and verifies that the fifth generation still compiles and runs a direct-fastc program.
116+
Self-host builds enter fastc's complete lane directly. The checked frontend feeds the independent
117+
`v3.gen.fastc.FlatGen` implementation, so the resulting compiler supports the full V3 source tree
118+
and retains both fastc lanes for user programs, including when its output has a custom filename in
119+
the V checkout. The fastc integration test exercises five successive self-host generations.
116120

117121
Generated C represents `thread` values with a typed wrapper around `pthread_t`. `spawn` and
118122
detached standard-library workers use the target's default thread stack (8 MiB on 64-bit targets

0 commit comments

Comments
 (0)