Repository navigation
V3: support asm{} blocks under -cc msvc via a MASM (ml64.exe) assemble-and-link step #29267
Replies: 1 comment
|
Scoping this before writing any code, since it changes codegen output shape and the build driver. What I measured and read, on master Today under There is no Proposal, as three small independent PRs (in this order):
Questions for the maintainers:
I can start with steps 1 and 2 if that direction is fine. |
Uh oh!
There was an error while loading. Please reload this page.
Description
asm amd64 { ... }blocks are currently excluded under-cc msvc(see the!msvcguards invlib/v/slow_tests/assembly/asm_no_tcc_test.amd64.vandvlib/v/slow_tests/assembly/asm_raw_intel_test.amd64.v), with the comment"msvc does not support inline assembly at all." That's accurate as stated:
MSVC's x64
cl.exehas never supported inline assembly in any form (only32-bit MSVC has the old, constraint-free
__asm { }block), and V's asmcodegen emits GNU extended inline assembly syntax, a GCC/Clang C-language
extension with no MSVC equivalent.
This issue proposes a path to real (not inline) assembly support under
-cc msvc, using the same pipeline Visual Studio/MSBuild itself uses forhandwritten assembly:
cl.execompiles the generated.c->.objml64.exe(MASM) assembles a generated.asm->.objlink.exemerges all.objs ->.exe/.dllMASM produces ordinary COFF objects, which
cl.exe/link.exealreadyconsume natively -- confirmed on a Windows box in this repo's CI-equivalent
environment that
ml64.exeships right next tocl.exe/link.exein astandard Visual Studio install
(
VC\Tools\MSVC\<ver>\bin\Hostx64\x64\ml64.exe).Why this isn't a mechanical reroute
The blocker isn't object-format compatibility -- it's that V's current asm
model is inherently inline: each block's operands are bound by GNU
extended-asm constraint strings (
=*m,=&r,rm,r, clobber lists)directly into the enclosing C function's own stack frame and registers.
MASM has no concept of "inline" at all; every MASM routine is a real,
separately-assembled, externally-linked function. Making this work under
msvc means changing the block from "runs inline, sharing my frame" to "is a
real function I now call," which touches several areas:
asm { }block becomes its own named MASMPROC/ENDPin a generated.asmfile instead of inline C text.calling convention.
rm (x)/r (y)-style inputs become actualparameters (RCX/RDX/R8/R9, then stack);
=*m (lo)/=&r (hi)-styleoutputs become return values or out-parameters written through pointers,
since there's no shared frame to write into directly anymore.
non-volatile under the Windows x64 ABI. A clobber that previously just
told GCC "don't assume this survives" now needs the generated
PROCtoactually save/restore it if it's one of those, since it's a real call
boundary rather than inlined text.
.objneed uniquesymbol names across blocks -- relevant to the goto-lowering work already
tracked in v3: harden asm goto lowering (case-insensitive mnemonics, lock-scope diagnostic) #28477.
with named operands, no fixed constraint shape) needs the same
ABI-boundary treatment to become callable, plus a translation from the
Intel-syntax templates V already supports there (MASM is Intel-syntax
natively, which helps) to a real proc signature.
v.driverneeds a new pipeline stage that shells outto
ml64.exefor the generated.asmand feeds its.objinto the samelink.exeinvocation CL's output already goes through.Open questions / risks
a real call boundary (e.g. anything implicitly relying on access to a
caller local not declared as an operand, if the checker permits that
today) -- these may need to be checker-rejected under
-cc msvcratherthan silently mistranslated.
ml64.exeships with a full Visual Studioinstall, but Windows CI images that only install the "Build Tools" C/C++
workload may not include it -- needs a locate-or-degrade-gracefully step
in the driver, similar to how
cl.exe/link.exeare already located.risk correctness surface: getting this wrong produces asm that appears to
work in simple cases and corrupts caller state under specific register
pressure, which is exactly the kind of bug that survives a first pass of
testing.
Scope
This is a multi-day compiler feature (new codegen path + build driver
change), not a small patch. Filing this as a design proposal / enhancement
request rather than attempting an implementation, so it can be scoped and
prioritized by the maintainers against V3's msvc backend roadmap
(
-cc msvcitself only recently became viable for ordinary programs via#28922).
All reactions