Skip to content

v.gen.arm64 #

ARM64 Instruction Encoding Helpers These functions provide type-safe instruction encoding for the ARM64 backend.

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.

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
	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
	cur_func_ret_type    ssa.TypeID
	cur_func_sret_offset int
	// prune_unused emits only the functions reachable from main (freestanding
	// objects, whose runtime registers 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
}

Gen stores state for ARM64 code generation.

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) gen_parallel #

fn (mut g Gen) gen_parallel()

gen_parallel emits independent contiguous function ranges concurrently and merges their text, symbols, strings, and relocations in source order.

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.

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
}

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 #

fn (mut m MachOObject) write(path string)

write supports write handling for MachOObject.