docs/codegen/code-stub-assembler.md
The CodeStubAssembler (CSA) is a critical component in V8's code generation pipeline. It provides a JavaScript-specific "macro-assembler" interface on top of V8's low-level compiler::CodeAssembler.
CSA is used to write low-level code, such as builtins (e.g., Promise implementation details, parts of Array.prototype methods) and bytecode handlers for Ignition. While many builtins are now written in Torque (which generates CSA code), understanding CSA is still essential for working on V8's low-level components. It allows writing code in a way that is portable across all architectures supported by V8 (x64, ARM, ARM64, MIPS, etc.) without having to write raw machine code for each platform.
For many complex operations (e.g., Float64Ceil, Float64Floor, PopulationCount), CSA checks if the target architecture supports native instructions for these operations (e.g., via IsFloat64RoundUpSupported()).
CSA abstracts away platform-specific details like pointer size, pointer compression, and Smi (Small Integer) representation.
SmiAdd) automatically handle whether Smis are 31-bit or 32-bit and whether they need to be shifted or masked, based on the build configuration.While it feels like assembly, it is strongly typed at the C++ level using TNode<T> (e.g., TNode<Smi>, TNode<IntPtrT>, TNode<Context>). This prevents many common low-level errors by ensuring that operations are only performed on compatible types at compile time.
CSA provides higher-level operations that are common in JavaScript execution but would be tedious to write in raw assembly:
AllocateInNewSpace).V8 now uses a domain-specific language called Torque for writing many of its builtins. Torque code (files with .tq extension) is compiled by the Torque compiler into C++ code that uses the CodeStubAssembler interface.
For more details on Torque, see the Torque documentation.
src/codegen/code-stub-assembler.h: Header file defining the CSA interface.src/codegen/code-stub-assembler.cc: Implementation of the CSA operations.