Skip to content

v2.ssa #

fn Builder.new #

fn Builder.new(mod &Module) &Builder

fn Builder.new_with_env #

fn Builder.new_with_env(mod &Module, env &types.Environment) &Builder

fn Module.new #

fn Module.new(name string) &Module

fn TypeStore.new #

fn TypeStore.new() &TypeStore

type BlockID #

type BlockID = int

type TypeID #

type TypeID = int

type ValueID #

type ValueID = int

enum AtomicOrdering #

enum AtomicOrdering {
	not_atomic
	unordered
	monotonic
	acquire
	release
	acq_rel
	seq_cst
}

enum CallConv #

enum CallConv {
	c_decl
	fast_call
	wasm_std
}

enum InlineHint #

enum InlineHint {
	none   // No hint, let optimizer decide
	always // Always inline (e.g., V's [inline] attribute)
	never  // Never inline (e.g., V's [noinline] attribute)
	hint   // Suggest inlining (optimizer may ignore)
}

enum Linkage #

enum Linkage {
	external
	private
	internal
}

enum OpCode #

enum OpCode {
	// Terminators
	ret
	br
	jmp
	switch_
	unreachable

	// Binary (integer)
	add
	sub
	mul
	sdiv
	udiv
	srem
	urem

	// Binary (float)
	fadd
	fsub
	fmul
	fdiv
	frem

	// Bitwise
	shl
	lshr
	ashr
	and_
	or_
	xor

	// Memory
	alloca
	load
	store
	get_element_ptr
	fence
	cmpxchg
	atomicrmw

	// Conversion
	trunc
	zext
	sext
	fptoui
	fptosi
	uitofp
	sitofp
	bitcast

	// Comparisons
	lt
	gt
	le
	ge
	eq
	ne

	// Other
	phi
	call
	call_indirect // Indirect call through function pointer
	call_sret     // Call with struct return (x8 indirect return on ARM64)
	select
	assign             // copy for phi elimination
	inline_string_init // Create string struct by value: (string){str, len, is_lit}

	// Aggregate (struct/tuple) operations
	extractvalue // Extract element from struct/tuple: extractvalue %tuple, index
	insertvalue  // Insert element into struct/tuple: insertvalue %tuple, %val, index
}

enum TypeKind #

enum TypeKind {
	void_t
	int_t
	float_t
	ptr_t
	array_t
	struct_t
	func_t
	label_t
	metadata_t
}

enum ValueKind #

enum ValueKind {
	unknown
	constant
	argument
	global
	instruction
	basic_block
	string_literal // V string struct literal (by value)
	func_ref       // Function pointer reference (for map hash/eq/clone/free functions)
}

struct BasicBlock #

struct BasicBlock {
pub:
	id     BlockID
	val_id ValueID
	name   string
	parent int // Function ID
pub mut:
	instrs []ValueID

	// Control Flow Graph
	preds []BlockID
	succs []BlockID

	// Dominators
	idom     BlockID
	dom_tree []BlockID
}

struct Builder #

struct Builder {
mut:
	mod           &Module
	cur_func      int     = -1
	cur_block     BlockID = -1
	cur_func_name string

	// Type checker environment with populated scopes
	env &types.Environment = unsafe { nil }

	// Current module being processed (for type lookups)
	cur_module string = 'main'

	// Maps AST variable name to SSA ValueID (pointer to stack slot)
	vars map[string]ValueID

	// Maps variable name to struct type name (for method resolution)
	var_struct_types map[string]string

	// Stack for break/continue targets
	loop_stack []LoopInfo
	// Function-local label target blocks for goto/label lowering.
	label_blocks map[string]BlockID

	// Maps struct name to TypeID
	struct_types map[string]TypeID

	// Current match expression type for enum shorthand
	cur_match_type string

	// Return type enum name for current function (for resolving enum shorthands in return stmts)
	cur_func_ret_enum_name string

	// Struct field enum types: "StructName.fieldName" -> enum type name
	struct_field_enum_types map[string]string

	// Enum types accessible in current function (from params, receiver fields, return type)
	cur_func_enum_context map[string]bool

	// Deferred statements for current function (executed in reverse order at return)
	defer_stmts [][]ast.Stmt

	// Maps enum value (EnumName__field) to integer value
	enum_values map[string]int

	// Maps variable name to array length (for fixed-size arrays)
	var_array_sizes map[string]int
	// Maps variable/selector key to dynamic array element type (for indexing loads/stores)
	var_array_elem_types map[string]TypeID

	// Maps variable name to map type info (key_bytes, value_bytes)
	var_map_types map[string][2]int // Variable name -> [key_bytes, value_bytes]
	// Maps variable name to map value type (for map indexing loads)
	var_map_value_types map[string]TypeID

	// Interface support
	interface_names      map[string]bool     // Track interface type names
	interface_meths      map[string][]string // Interface name -> method names
	type_methods         map[string][]string // Type name -> method names (for vtable)
	iface_concrete_types map[string]string   // Variable name -> concrete type (for interface vars)

	// Expression type tracking (v2.types integration)
	var_types      map[string]types.Type // Variable name -> v2.types.Type
	func_ret_types map[string]TypeID     // Function name -> return TypeID
	// Guard against pathological recursive type lowering.
	type_to_ssa_depth int

	// Function pointer field support
	fn_type_aliases map[string]bool // Type names that are function types (e.g., MapEqFn)
	fn_ptr_fields   map[string]bool // Struct.field combinations that are function pointers

	// Type alias resolution for method calls (e.g., Builder -> array)
	type_alias_bases map[string]string // Type alias name -> base type name for methods
	// Preserve alias-typed struct fields for method dispatch (e.g., Gen.sb -> Builder).
	field_alias_types map[string]string // "." -> alias type name

	// Enum type names for cast detection
	enum_names map[string]bool

	// Flag enum type names for shorthand resolution
	flag_enum_names map[string]bool

	// Module tracking for types (type_name -> module_name)
	type_modules map[string]string

	// Sum type variant mapping: SumTypeName -> ordered variant TypeIDs.
	sumtype_variants map[string][]TypeID
	// Sum type variant ASTs captured during registration and resolved once
	// all structs/enums are registered.
	sumtype_variant_exprs map[string][]ast.Expr

	// Used-function keys from markused stage.
	used_fn_keys map[string]bool

	// Ordered global initializers emitted into a synthetic init function.
	global_init_entries []GlobalInitEntry
	in_global_init      bool
}

fn (Builder) set_used_fn_keys #

fn (mut b Builder) set_used_fn_keys(used map[string]bool)

set_used_fn_keys sets the declaration keys that should be emitted.

fn (Builder) build_all #

fn (mut b Builder) build_all(files []ast.File)

build_all processes multiple files with proper multi-file ordering:1. Register all types from all files first2. Register all function signatures from all files3. Generate all function bodies

fn (Builder) build_types #

fn (mut b Builder) build_types(file ast.File)

build_types registers struct types, globals, enums from a single file

fn (Builder) build_fn_signatures #

fn (mut b Builder) build_fn_signatures(file ast.File)

build_fn_signatures registers function signatures from a single file

fn (Builder) build_fn_bodies #

fn (mut b Builder) build_fn_bodies(file ast.File)

build_fn_bodies generates function bodies from a single file

fn (Builder) build #

fn (mut b Builder) build(file ast.File)

build processes a single file (legacy method for backward compatibility)

struct ConstantData #

struct ConstantData {
pub:
	int_val   i64
	float_val f64
	str_val   string
}

struct Function #

struct Function {
pub:
	id   int
	name string
	typ  TypeID
pub mut:
	blocks []BlockID
	params []ValueID

	linkage   Linkage
	call_conv CallConv
}

struct GlobalVar #

struct GlobalVar {
pub:
	name          string
	typ           TypeID
	linkage       Linkage
	alignment     int
	is_constant   bool
	initial_value i64 // For constants/enums, the initial integer value
}

struct Instruction #

struct Instruction {
pub mut:
	op OpCode
	// Operands are IDs of other Values
	operands []ValueID
pub:
	block BlockID
	typ   TypeID // Result type

	pos        token.Pos
	atomic_ord AtomicOrdering
	inline     InlineHint // Inline hint for call instructions
}

struct Module #

@[heap]
struct Module {
pub mut:
	name       string
	target     TargetData
	type_store TypeStore

	// Type checker environment (optional, for backends that need rich type info)
	env &types.Environment = unsafe { nil }

	// Arenas
	values  []Value
	instrs  []Instruction
	blocks  []BasicBlock
	funcs   []Function
	globals []GlobalVar

	// C struct names: TypeID -> C struct name (e.g., 114 -> "stat" for struct stat)
	// Used by the C gen to emit `typedef struct  Struct_N;` instead of
	// generating a custom struct definition that would have the wrong memory layout.
	c_struct_names map[int]string
	// C structs marked with @[typedef] – already a C typedef, not a struct tag.
	c_typedef_structs map[int]bool

	// Constant cache: (type, name) -> ValueID for deduplication
	const_cache map[string]ValueID
}

fn (Module) new_function #

fn (mut m Module) new_function(name string, ret TypeID, params []TypeID) int

fn (Module) add_block #

fn (mut m Module) add_block(func_id int, name string) BlockID

fn (Module) add_value_node #

fn (mut m Module) add_value_node(kind ValueKind, typ TypeID, name string, index int) ValueID

Updated to accept 'index'

fn (Module) get_or_add_const #

fn (mut m Module) get_or_add_const(typ TypeID, name string) ValueID

Get or create a constant value, reusing existing ones when possible. This maintains SSA's immutability principle by avoiding duplicate constants.

fn (Module) get_block_from_val #

fn (m Module) get_block_from_val(val_id int) int

fn (Module) add_instr #

fn (mut m Module) add_instr(op OpCode, block BlockID, typ TypeID, operands []ValueID) ValueID

fn (Module) add_global #

fn (mut m Module) add_global(name string, typ TypeID, is_const bool) int

fn (Module) add_global_with_value #

fn (mut m Module) add_global_with_value(name string, typ TypeID, is_const bool, initial_value i64) int

fn (Module) add_external_global #

fn (mut m Module) add_external_global(name string, typ TypeID) ValueID

add_external_global adds an external global variable (defined outside this module) Returns the ValueID for the global pointer

fn (Module) add_instr_front #

fn (mut m Module) add_instr_front(op OpCode, block BlockID, typ TypeID, operands []ValueID) ValueID

fn (Module) replace_uses #

fn (mut m Module) replace_uses(old_val int, new_val int)

struct TargetData #

struct TargetData {
pub:
	ptr_size      int
	endian_little bool
}

struct Type #

struct Type {
pub:
	kind        TypeKind
	width       int      // Bit width
	elem_type   TypeID   // For Ptr, Array
	len         int      // For Array
	fields      []TypeID // For Structs
	field_names []string // Field names for Structs
	params      []TypeID // For Funcs
	ret_type    TypeID
	is_c_struct bool // True for C interop structs (use raw field names, typedef to C struct)
}

struct TypeStore #

struct TypeStore {
pub mut:
	types []Type
	cache map[string]TypeID
}

fn (TypeStore) get_int #

fn (mut ts TypeStore) get_int(width int) TypeID

fn (TypeStore) get_float #

fn (mut ts TypeStore) get_float(width int) TypeID

fn (TypeStore) get_ptr #

fn (mut ts TypeStore) get_ptr(elem TypeID) TypeID

fn (TypeStore) get_array #

fn (mut ts TypeStore) get_array(elem TypeID, length int) TypeID

fn (TypeStore) get_tuple #

fn (mut ts TypeStore) get_tuple(elem_types []TypeID) TypeID

fn (TypeStore) register #

fn (mut ts TypeStore) register(t Type) TypeID

struct Value #

struct Value {
pub:
	id  ValueID
	typ TypeID
	// Index into the specific arena (instrs, blocks, globals)
	index int
pub mut:
	kind ValueKind
	name string
	uses []ValueID
}