Files
sx/src/ir/inst.zig
agra 6a3260ff65 ffi 2.16c green: TL fallback via C-helper runtime + always-omit env in #jni_call
`#jni_call` collapses to a single surface — env is *always* implicit:
either picked up from the lexically-enclosing `#jni_env(env) { ... }`
block's Ref (cheap, register-resident, no TL touch) or from the
runtime's thread-local slot via `sx_jni_env_tl_get()` (one fn call
per dispatch). The explicit-env shape is gone — chess and the
existing tests migrate cleanly by wrapping their helper-fn bodies
in `#jni_env(env) { ... }`.

The TL slot lives outside the user's IR module so the LLVM ORC JIT
can load object files cleanly without `orc_rt` for TLS support:

  library/vendors/sx_jni_runtime/sx_jni_env_tl.c:
    static _Thread_local void *sx_jni_env_tl_slot;
    void *sx_jni_env_tl_get(void) { return sx_jni_env_tl_slot; }
    void sx_jni_env_tl_set(void *env) { sx_jni_env_tl_slot = env; }

Linkage:
- sx-the-compiler links the .c file via build.zig so the JIT
  process-symbol generator resolves `sx_jni_env_tl_get`/`_set`.
- AOT targets get the same .c file auto-linked via the lowering
  pass: when lower touches the TL externs, it sets
  `needs_jni_env_tl_runtime`, and `Compilation.lowerToIR` appends a
  synthetic `CImportInfo` to `lowering_extra_c_sources` that
  `collectCImportSources` merges with user-written ones.

Lowering-side changes:
- `getJniEnvTlFids` lazily declares the two externs (parallel
  to `getSelRegisterNameFid`) and flips `needs_jni_env_tl_runtime`.
- `#jni_env(env) { body }` emits save→set→body→restore via three
  `call` ops to the externs; the inner body sees env via the
  lexical-direct stack.
- `lowerJniCall` resolves env from `jni_env_stack` (top) or the TL
  fallback. The explicit-env branch is gone.
- `jni_env_stack_base` tracks per-fn lexical scope so lazy-lowering
  a callee doesn't accidentally see the caller's Ref (Refs are only
  valid inside one fn's instruction stream).

Test migration (mechanical):
- ffi-jni-call-{01..09}: each helper fn wraps `#jni_call(...)`
  bodies in `#jni_env(env) { ... }`. Returning values pass through
  the block as an expression — `#jni_env` now also lowers in
  expression position.

Verified:
- zig build test + tests/run_examples.sh: 130/130 green.
- tests/cross_compile.sh: 3/3 green.
- Chess APK rebuilt + reinstalled on Pixel. Board renders with
  status-bar clearance + info panel intact; no crashes in logcat.
  Safe-insets dispatch through `#jni_env` + lexical-direct now
  fully exercised end-to-end on real hardware.
2026-05-20 13:53:25 +03:00

529 lines
18 KiB
Zig

const std = @import("std");
const types = @import("types.zig");
const TypeId = types.TypeId;
const StringId = types.StringId;
// ── Handles ─────────────────────────────────────────────────────────────
/// Reference to an SSA value (instruction result).
pub const Ref = enum(u32) {
/// Sentinel for "no value" / unused operand.
none = std.math.maxInt(u32),
_,
pub fn index(self: Ref) u32 {
return @intFromEnum(self);
}
pub fn fromIndex(i: u32) Ref {
return @enumFromInt(i);
}
pub fn isNone(self: Ref) bool {
return self == .none;
}
};
pub const BlockId = enum(u32) {
_,
pub fn index(self: BlockId) u32 {
return @intFromEnum(self);
}
pub fn fromIndex(i: u32) BlockId {
return @enumFromInt(i);
}
};
pub const FuncId = enum(u32) {
_,
pub fn index(self: FuncId) u32 {
return @intFromEnum(self);
}
pub fn fromIndex(i: u32) FuncId {
return @enumFromInt(i);
}
};
pub const GlobalId = enum(u32) {
_,
pub fn index(self: GlobalId) u32 {
return @intFromEnum(self);
}
pub fn fromIndex(i: u32) GlobalId {
return @enumFromInt(i);
}
};
// ── Span ────────────────────────────────────────────────────────────────
pub const Span = struct {
start: u32 = 0,
end: u32 = 0,
};
// ── Instruction ─────────────────────────────────────────────────────────
pub const Inst = struct {
op: Op,
ty: TypeId,
span: Span = .{},
};
// ── Op (tagged union) ───────────────────────────────────────────────────
pub const Op = union(enum) {
// ── Constants ───────────────────────────────────────────────────
const_int: i64,
const_float: f64,
const_bool: bool,
const_string: StringId,
const_null,
const_undef, // `---` undefined initializer
// ── Arithmetic ──────────────────────────────────────────────────
add: BinOp,
sub: BinOp,
mul: BinOp,
div: BinOp,
mod: BinOp,
neg: UnaryOp, // unary -x
// ── Bitwise ─────────────────────────────────────────────────────
bit_and: BinOp,
bit_or: BinOp,
bit_xor: BinOp,
bit_not: UnaryOp,
shl: BinOp,
shr: BinOp,
// ── Comparison ──────────────────────────────────────────────────
cmp_eq: BinOp,
cmp_ne: BinOp,
cmp_lt: BinOp,
cmp_le: BinOp,
cmp_gt: BinOp,
cmp_ge: BinOp,
str_eq: BinOp, // string/slice equality via memcmp
str_ne: BinOp, // string/slice inequality via memcmp
// ── Logical ─────────────────────────────────────────────────────
bool_and: BinOp, // short-circuit &&
bool_or: BinOp, // short-circuit ||
bool_not: UnaryOp,
// ── Conversions ─────────────────────────────────────────────────
widen: Conversion, // safe widening (s32 → s64)
narrow: Conversion, // truncation via `xx` (s64 → s32)
bitcast: Conversion, // reinterpret bits
int_to_float: Conversion,
float_to_int: Conversion,
// ── Memory ──────────────────────────────────────────────────────
alloca: TypeId, // stack allocation, result is *T
load: UnaryOp, // load from pointer
store: Store, // store value to pointer
heap_alloc: UnaryOp, // context.allocator.alloc(size) → *void
heap_free: UnaryOp, // context.allocator.free(ptr)
// ── Struct ops ──────────────────────────────────────────────────
struct_init: Aggregate, // construct struct from field values
struct_get: FieldAccess, // read struct field by index
struct_gep: FieldAccess, // get pointer to struct field (GEP)
// ── Enum ops ────────────────────────────────────────────────────
enum_init: EnumInit, // construct enum value (tag + optional payload)
enum_tag: UnaryOp, // extract tag from enum/union
enum_payload: FieldAccess, // extract payload from tagged union
// ── Union ops ───────────────────────────────────────────────────
union_get: FieldAccess, // read union field (reinterpret)
union_gep: FieldAccess, // pointer to union field
// ── Array/Slice ops ─────────────────────────────────────────────
index_get: BinOp, // arr[idx] → value
index_gep: BinOp, // &arr[idx] → pointer
length: UnaryOp, // .len on slice/string/array
data_ptr: UnaryOp, // .ptr on slice/string
subslice: Subslice, // arr[lo..hi]
array_to_slice: UnaryOp, // [N]T → []T
// ── Tuple ops ───────────────────────────────────────────────────
tuple_init: Aggregate, // construct tuple from values
tuple_get: FieldAccess, // read tuple element by index
// ── Optional ops ────────────────────────────────────────────────
optional_wrap: UnaryOp, // T → ?T
optional_unwrap: UnaryOp, // ?T → T (UB if null)
optional_has_value: UnaryOp, // ?T → bool
optional_coalesce: BinOp, // a ?? b
// ── Pointer ops ─────────────────────────────────────────────────
addr_of: UnaryOp, // @x → *T
deref: UnaryOp, // p.* → T
// ── Vector ops ──────────────────────────────────────────────────
vec_splat: UnaryOp, // scalar → vector (broadcast)
vec_extract: BinOp, // vec[idx] → scalar
vec_insert: TriOp, // vec, idx, val → new_vec
// ── Calls ───────────────────────────────────────────────────────
call: Call,
call_indirect: CallIndirect,
call_closure: CallIndirect,
call_builtin: BuiltinCall,
compiler_call: CompilerCall,
/// `#objc_call(ReturnT)(recv, sel, args...)` — dispatched through
/// `objc_msgSend`. emit_llvm.zig synthesizes a per-call-site LLVM
/// function type from the arg/result Refs and reuses a single
/// declared `@objc_msgSend` symbol across all return-type
/// variants. Encoded as its own opcode (instead of `.call` /
/// `.call_indirect`) so the IR doesn't need a separate FuncId
/// per signature shape.
objc_msg_send: ObjcMsgSend,
/// `#jni_call(ReturnT)(env, target, name, sig, args...)` and
/// `#jni_static_call(ReturnT)(env, class, name, sig, args...)`.
/// emit_llvm.zig expands this into the JNI vtable indirection:
/// `(*env)->GetObjectClass` (instance only) → `GetMethodID` /
/// `GetStaticMethodID` → `Call<Type>Method` / `CallStatic<Type>Method`.
/// Method-ID caching across call sites is added in step 1.17.
jni_msg_send: JniMsgSend,
// ── Protocol dispatch ───────────────────────────────────────────
protocol_call_dynamic: ProtocolCall, // vtable/inline dispatch
protocol_erase: ProtocolErase, // concrete → protocol value (xx)
// ── Closure creation ────────────────────────────────────────────
closure_create: ClosureCreate,
// ── Context ─────────────────────────────────────────────────────
context_load: ContextOp, // read context field
context_store: ContextOp, // write context field
context_save, // save context state (for push)
context_restore: UnaryOp, // restore context state (after push)
// ── Globals ─────────────────────────────────────────────────────
global_get: GlobalId,
global_addr: GlobalId, // address of a global (pointer, not load)
global_set: GlobalSet,
func_ref: FuncId, // reference to a function (for function pointers)
// ── Block params (SSA phi alternative) ──────────────────────────
block_param: BlockParam,
// ── Any type ────────────────────────────────────────────────────
box_any: BoxAny, // T → Any (erase type)
unbox_any: UnaryOp, // Any → T (restore type)
// ── Reflection ─────────────────────────────────────────────────
field_name_get: FieldReflect, // field_name(T, i) → string (runtime index)
field_value_get: FieldReflect, // field_value(s, i) → Any (runtime struct + index)
// ── Terminators ─────────────────────────────────────────────────
br: Branch,
cond_br: CondBranch,
switch_br: SwitchBranch,
ret: UnaryOp,
ret_void,
@"unreachable",
// ── Misc ────────────────────────────────────────────────────────
/// No-op placeholder for unlowered AST nodes.
placeholder: StringId, // name of the unlowered construct
};
// ── Operand structs ─────────────────────────────────────────────────────
pub const UnaryOp = struct {
operand: Ref,
};
pub const BinOp = struct {
lhs: Ref,
rhs: Ref,
};
pub const TriOp = struct {
a: Ref,
b: Ref,
c: Ref,
};
pub const Store = struct {
ptr: Ref,
val: Ref,
};
pub const Conversion = struct {
operand: Ref,
from: TypeId,
to: TypeId,
};
pub const FieldAccess = struct {
base: Ref,
field_index: u32,
/// The IR type of the aggregate being accessed (struct, union, etc.).
/// Used by the LLVM emitter to resolve the correct type for GEP operations
/// without guessing from LLVM value chains.
base_type: ?TypeId = null,
};
pub const Aggregate = struct {
fields: []const Ref,
};
pub const EnumInit = struct {
tag: u32,
payload: Ref, // Ref.none if no payload
};
pub const Subslice = struct {
base: Ref,
lo: Ref,
hi: Ref,
};
pub const Call = struct {
callee: FuncId,
args: []const Ref,
};
pub const CallIndirect = struct {
callee: Ref,
args: []const Ref,
};
/// `#objc_call` dispatch through `objc_msgSend`. emit_llvm reads
/// `recv`/`sel`/each arg's IR type to build the per-call-site LLVM
/// function type; the instruction's own `ty` field (`Inst.ty`) is the
/// Obj-C return type. One declared `@objc_msgSend` symbol is shared
/// across every distinct signature shape.
pub const ObjcMsgSend = struct {
recv: Ref,
sel: Ref,
args: []const Ref, // additional args after recv + sel
};
/// JNI dispatch payload. `env` is `JNIEnv*` (typed as ptr); `target`
/// is a `jobject` for instance calls and a `jclass` for static calls.
/// `name` and `sig` are pointers to NUL-terminated bytes (typically
/// `[*]u8` from a string-literal `.ptr`). When the source-level
/// `name` and `sig` are string literals, `cache_key` carries their
/// content so emit_llvm.zig can intern a shared `jclass GlobalRef` +
/// `jmethodID` slot keyed on `(name, sig)`; otherwise the lookup
/// stays uncached. The dispatch sequence is expanded in
/// emit_llvm.zig — see `Inst.jni_msg_send`.
pub const JniMsgSend = struct {
env: Ref,
target: Ref,
name: Ref,
sig: Ref,
args: []const Ref,
is_static: bool,
cache_key: ?CacheKey = null,
};
pub const CacheKey = struct {
name_str: []const u8,
sig_str: []const u8,
};
pub const BuiltinCall = struct {
builtin: BuiltinId,
args: []const Ref,
};
pub const BuiltinId = enum(u16) {
out,
sqrt,
sin,
cos,
floor,
size_of,
cast,
malloc,
free,
memcpy,
memset,
type_of,
alloc,
dealloc,
};
pub const CompilerCall = struct {
name: u32, // StringPool id for qualified name (e.g. "BuildOptions.add_link_flag")
args: []const Ref,
};
pub const ProtocolCall = struct {
receiver: Ref, // protocol value (ctx + vtable/fn_ptrs)
method_index: u32,
args: []const Ref,
};
pub const ProtocolErase = struct {
concrete: Ref,
protocol_type: TypeId,
};
pub const ClosureCreate = struct {
func: FuncId, // trampoline function
env: Ref, // allocated env pointer (or Ref.none for no captures)
};
pub const ContextOp = struct {
field: StringId,
value: Ref, // Ref.none for loads
};
pub const GlobalSet = struct {
global: GlobalId,
value: Ref,
};
pub const BlockParam = struct {
block: BlockId,
param_index: u32,
};
pub const BoxAny = struct {
operand: Ref,
source_type: TypeId,
};
pub const FieldReflect = struct {
base: Ref, // struct value (for field_value_get) or Ref.none (for field_name_get)
index: Ref, // runtime field index
struct_type: TypeId, // compile-time resolved struct type
};
pub const Branch = struct {
target: BlockId,
args: []const Ref, // block param values
};
pub const CondBranch = struct {
cond: Ref,
then_target: BlockId,
then_args: []const Ref,
else_target: BlockId,
else_args: []const Ref,
};
pub const SwitchBranch = struct {
operand: Ref,
cases: []const Case,
default: BlockId,
default_args: []const Ref,
pub const Case = struct {
value: i64,
target: BlockId,
args: []const Ref,
};
};
// ── Block ───────────────────────────────────────────────────────────────
pub const Block = struct {
name: StringId,
params: []const TypeId, // block parameter types (SSA phi alternative)
insts: std.ArrayList(Inst),
first_ref: u32 = 0, // ref index of the first instruction in this block
pub fn init(name: StringId, params: []const TypeId) Block {
return .{
.name = name,
.params = params,
.insts = std.ArrayList(Inst).empty,
};
}
pub fn deinit(self: *Block, alloc: std.mem.Allocator) void {
self.insts.deinit(alloc);
}
};
// ── Function ────────────────────────────────────────────────────────────
pub const Function = struct {
name: StringId,
params: []const Param,
ret: TypeId,
blocks: std.ArrayList(Block),
is_extern: bool = false,
is_comptime: bool = false,
linkage: Linkage = .internal,
call_conv: CallingConvention = .default,
source_file: ?[]const u8 = null,
pub const Param = struct {
name: StringId,
ty: TypeId,
};
pub const Linkage = enum {
internal,
external,
private,
};
pub const CallingConvention = types.TypeInfo.CallConv;
pub fn init(name: StringId, params: []const Param, ret: TypeId) Function {
return .{
.name = name,
.params = params,
.ret = ret,
.blocks = std.ArrayList(Block).empty,
};
}
pub fn deinit(self: *Function, alloc: std.mem.Allocator) void {
for (self.blocks.items) |*block| {
block.deinit(alloc);
}
self.blocks.deinit(alloc);
}
};
// ── Global ──────────────────────────────────────────────────────────────
pub const Global = struct {
name: StringId,
ty: TypeId,
init_val: ?ConstantValue = null,
is_extern: bool = false,
is_const: bool = false,
/// Thread-local storage. `global_get` / `global_set` emit normal LLVM
/// load/store instructions; LLVM handles the per-thread access through
/// the `thread_local` attribute on the global.
is_thread_local: bool = false,
/// For comptime globals: the function to interpret to get the init value.
comptime_func: ?FuncId = null,
};
// ── ConstantValue ───────────────────────────────────────────────────────
pub const ConstantValue = union(enum) {
int: i64,
float: f64,
boolean: bool,
string: StringId,
null_val,
undef,
zeroinit,
aggregate: []const ConstantValue,
/// Vtable constant: struct of function pointers, used for protocol vtable globals.
vtable: []const FuncId,
};