v.gen.arm64 #
ARM64 Instruction Encoding Helpers These functions provide type-safe instruction encoding for the ARM64 backend.
fn CFunctionKind.from #
fn CFunctionKind.from[W](input W) !CFunctionKind
from returns the CFunctionKind value of an integer or of a field name, or an error if there is none.
fn Gen.new #
fn Gen.new(m &ssa.Module) &Gen
new creates a Gen value for arm64.
fn Linker.new #
fn Linker.new(macho &MachOObject) &Linker
new creates a Linker value for arm64.
fn MachOObject.new #
fn MachOObject.new() &MachOObject
new creates a MachOObject value for arm64.
enum CFunctionKind #
enum CFunctionKind {
macro // a function-like macro
internal // a function with internal linkage (`static`, `static inline`)
}
CFunctionKind says why a C function needs a wrapper.
struct Gen #
struct Gen {
mut:
m &ssa.Module = unsafe { nil }
macho &MachOObject = unsafe { nil }
text_words []u32
stack_offsets []i32
alloca_offsets []i32
alloca_sizes []i32
alloca_alignments []i32
slot_value_indices []int
slot_value_base int
slot_value_count int
stack_size int
saved_callee_pairs int
saved_float_pairs int
current_instr_start int
value_homes []u8
value_home_ids []int
// Register allocation tables, reused across functions (see regalloc.v).
ra_interval []int
ra_value_block []int
ra_block_index []int
ra_block_first []int
ra_block_last []int
ra_pred_start []int
ra_preds []int
ra_live_stamp []int
ra_def_stamp []int
ra_stamp int
ra_list []LiveInterval
ra_live []LiveInterval
ra_integers []LiveInterval
ra_floats []LiveInterval
ra_crossing []int
ra_use_heads []int
ra_def_heads []int
ra_links []RaLink
ra_worklist []int
transient_stack_size int
block_offsets []i32
block_offset_indices []int
block_offset_base int
block_offset_count int
pending_jmps []PendingJmp
fn_offsets map[string]int
string_cache map[string]int // string content -> its symbol
chunk_strings []string // contents, in the order of their symbols
global_index map[string]int
global_index_ready bool
cur_func_ret_type ssa.TypeID
cur_func_sret_offset int
// export_entries holds the C names whose C-convention entry
// gen_export_entries emitted (see export_abi.v).
export_entries map[string]bool
// prune_unused emits only the functions reachable from main and exports
// (freestanding objects register helpers the program may never call).
pub mut:
prune_unused bool
// freestanding links the program's own runtime definitions of libc-named
// symbols (see Linker.freestanding).
freestanding bool
// gc_runtime sends libc allocation and thread calls to the Boehm helpers
// (see arm64_gc_runtime_symbols); set when the collector is linked in.
gc_runtime bool
}
Gen stores state for ARM64 code generation.
fn (Gen) c_extern_call_names #
fn (g &Gen) c_extern_call_names() []string
c_extern_call_names returns the C functions that V code calls or takes the address of, without the ones already bound to another symbol.
fn (Gen) c_function_wrappers #
fn (mut g Gen) c_function_wrappers(kinds map[string]CFunctionKind) string
c_function_wrappers returns the C source of the wrappers for kinds and binds the generator's calls to them. Functions whose calls cannot be spelled in C (variadic ones, or ones V code never calls directly) are left alone; the link reports them.
fn (Gen) write_elf #
fn (mut g Gen) write_elf(path string) !
write_elf writes the generated ARM64 code as an experimental Linux object.
fn (Gen) gen #
fn (mut g Gen) gen()
gen supports gen handling for Gen.
fn (Gen) write_and_link #
fn (mut g Gen) write_and_link(output string)
write_and_link writes and link output for arm64.
fn (Gen) text_exceeds_branch_range #
fn (g &Gen) text_exceeds_branch_range() bool
text_exceeds_branch_range reports whether the code is too large for the bl relocations the built-in linker can resolve, which reach 128 MiB.
fn (Gen) write_object #
fn (mut g Gen) write_object(output string) !
write_object writes the generated code as a relocatable Mach-O object for the system linker (external linking).
fn (Gen) gen_parallel #
fn (mut g Gen) gen_parallel(pool &workers.Pool)
gen_parallel generates the functions on pool, plus the calling thread, and produces the same object as gen. Module state must be frozen before workers share it, so this runs after the SSA module is final.
fn (Gen) extract_function #
fn (mut g Gen) extract_function(fn_name string) []u8
extract_function returns the raw machine code bytes of a single function, bounded by the next symbol in the text section. It is used for hot code reloading: the extracted bytes are written to a file, then loaded into an executable page of the running binary and called via a function pointer. Ported from v2 (vlib/v2_toberemoved/gen/arm64/arm64.v).
Only self-contained functions are supported: if the function body needs address fixups at its load address (calls, string literals, globals), the raw bytes cannot be relocated by a plain copy, so extraction is rejected. This covers both recorded relocations and direct PC-relative instructions that carry no relocation entry. Callers (notably -hot-fn) treat an empty result as a build error.
struct Linker #
struct Linker {
macho &MachOObject
// Frameworks to link (e.g. ['Metal', 'Cocoa', 'QuartzCore'])
pub mut:
frameworks []string
// freestanding keeps the program's own definitions of libc-named symbols
// (the `-freestanding` runtime's malloc/free/memcpy/exit/...) instead of
// forcing them to libSystem.
freestanding bool
// Output buffer
mut:
buf []u8
// Segment/section info
text_vmaddr u64
text_fileoff int
text_size int
data_vmaddr u64
data_fileoff int
data_size int
linkedit_off int
linkedit_size int
// External symbols needing binding
extern_syms []string
// GOT entries for external symbols
got_offset int // Offset within __DATA segment
got_size int
// Stubs for external function calls
stubs_offset int
stubs_size int
// Symbol to GOT index mapping
sym_to_got map[string]int
// Multi-dylib support: dylib paths and per-symbol ordinal mapping
dylibs []string // ['/usr/lib/libSystem.B.dylib', '/usr/lib/libobjc.A.dylib', ...]
sym_to_dylib map[string]int // symbol name → index into dylibs[] (ordinal = idx + 1)
// Code start offset (after header + load commands)
code_start int
// force_external_syms entries that a freestanding program defines itself
local_runtime_syms map[string]bool
}
Linker represents linker data used by arm64.
fn (Linker) link #
fn (mut l Linker) link(output_path string, entry_name string)
link supports link handling for Linker.
struct MachOObject #
struct MachOObject {
pub mut:
text_data []u8
str_data []u8
data_data []u8
relocs []RelocationInfo
symbols []Symbol
str_table []u8
sym_by_name map[string]int // symbol name → index in symbols
// callee-saved register pairs each function saves, for `__compact_unwind`
unwind_saved_pairs map[string]int
}
MachOObject represents mach oobject data used by arm64.
fn (MachOObject) write_elf #
fn (mut m MachOObject) write_elf(path string) !
write_elf serializes the already-generated Mach-O object data as an ELF64 relocatable object, so the ARM64 backend can target Linux/AArch64.
fn (MachOObject) add_symbol #
fn (mut m MachOObject) add_symbol(name string, addr u64, is_ext bool, sect u8) int
add_symbol updates add symbol state for MachOObject.
fn (MachOObject) add_undefined #
fn (mut m MachOObject) add_undefined(name string) int
add_undefined updates add undefined state for MachOObject.
fn (MachOObject) add_reloc #
fn (mut m MachOObject) add_reloc(addr int, sym_idx int, typ int, pcrel bool)
add_reloc updates add reloc state for MachOObject.
fn (MachOObject) write_object #
fn (mut m MachOObject) write_object(path string, opts MachOObjectOptions) !
write_object writes a relocatable MH_OBJECT that Apple's ld (or ld64.lld) can link together with C objects, static archives, dylibs and frameworks. This is the "external linking" mode (like Go's -linkmode=external): the built-in linker is still used for programs that need nothing but libSystem.
Layout follows what clang emits for arm64:
- sections __TEXT,__text, __TEXT,__const (string literals) and __DATA,__data, at the same addresses the generator used for its symbol values;
- symbols ordered locals, external definitions, undefined, as LC_DYSYMTAB requires;
- LC_BUILD_VERSION, so the linker knows the platform and does not warn.
String literals go to a regular __const section instead of __cstring: the linker splits S_CSTRING_LITERALS at NUL bytes and may merge or move the pieces, which breaks V literals that contain NULs or are referenced with an offset.
Hosted V wrappers such as malloc or exit share their name with the libc function they call. The built-in linker sends every reference to those names to libSystem; the object does the same by renaming the V definitions to private symbols, so neither V code nor C libraries linked next to it can recurse into the wrapper.
Like Go's machosymtab1 (cmd/link/internal/ld/macho.go), which marks a symbol N_EXT only when it is exported, V's own definitions stay local to the object: fn print or a runtime helper with the name of a C function (v3_pthread_create and pthread_helper.o) must not clash with C code linked next to it. Only opts.exported names and the program's own libc-named definitions stay external.
struct MachOObjectOptions #
struct MachOObjectOptions {
pub:
// freestanding keeps the program's own definitions of libc-named symbols.
freestanding bool
// exported lists the symbols C code may reference: `_main` and the
// `@[export]` names.
exported map[string]bool
// min_os is the minimum macOS version recorded in LC_BUILD_VERSION, as
// `major.minor` (default 11.0, the first macOS release for Apple silicon).
min_os string = '11.0'
}
MachOObjectOptions configures how write_object turns the generated code into a relocatable object for the system linker.