Intrinsics Reference

This page documents every @intrinsic the Gruel compiler recognizes. It is generated from the [gruel-intrinsics] registry (see ADR-0050); any changes must be made in Rust, not here.

Quick Reference

IntrinsicKindCategoryPreviewUncheckedSummary
@dbgexprDebug & DiagnosticsPrint values to stderr with a trailing newline.
@panicexprDebug & DiagnosticsAbort the program with an optional message.
@assertexprDebug & DiagnosticsCheck a boolean condition; panic if false.
@compile_errorexprCompile-time ReflectionEmit a compile-time error.
@castexprType CastsNumeric type conversion.
@read_lineexprI/ORead one line from stdin.
@parse_i32exprString ParsingParse a String into i32.
@parse_i64exprString ParsingParse a String into i64.
@parse_u32exprString ParsingParse a String into u32.
@parse_u64exprString ParsingParse a String into u64.
@random_u32exprRandom NumbersUniform random 32-bit integer.
@random_u64exprRandom NumbersUniform random 64-bit integer.
@size_oftypeCompile-time ReflectionSize of a type in bytes.
@align_oftypeCompile-time ReflectionAlignment of a type in bytes.
@type_nametypeCompile-time ReflectionName of a type as a comptime string.
@type_infotypeCompile-time ReflectionReflective info about a type.
@fieldexprCompile-time ReflectionAccess a field by comptime-known name.
@importexprCompile-time ReflectionImport another source file (placeholder).
@target_archexprTarget PlatformCompile target CPU architecture.
@target_osexprTarget PlatformCompile target operating system.
@ptr_readexprRaw PointersyesLoad a value through a raw pointer.
@ptr_writeexprRaw PointersyesStore a value through a raw mutable pointer.
@ptr_offsetexprRaw PointersyesPointer arithmetic by element count.
@ptr_to_intexprRaw PointersyesConvert a pointer to its integer address.
@int_to_ptrexprRaw PointersyesConstruct a pointer from an integer address.
@null_ptrexprRaw PointersyesA null pointer of the inferred type.
@is_nullexprRaw PointersyesTest whether a pointer is null.
@ptr_copyexprRaw PointersyesBulk copy between pointers.
@rawexprRaw PointersyesTake a const pointer to an lvalue.
@raw_mutexprRaw PointersyesTake a mutable pointer to an lvalue.
@syscallexprSystem CallsyesDirect OS system call.
@rangeexprIterationIterable range for for-loops.
@test_preview_gateexprPreview / Metatest_infraTest hook for the preview-feature gate.

Debug & Diagnostics

@dbg

@dbg(v1, v2, ...) prints each argument separated by spaces, then a newline. Accepts integers, bools, and String values.

Examples:

@dbg(42, true, "hello")

@panic

@panic() or @panic("message") terminates the program. Diverges (returns Never).

Examples:

@panic("unreachable")

@assert

@assert(cond) panics with a diagnostic if cond is false. Elided in release builds (future work).

Examples:

@assert(x > 0)

Type Casts

@cast

@cast(x) converts between integer and/or float types. The target type is inferred from context.

Examples:

let y: i64 = @cast(x);

I/O

@read_line

@read_line() returns a String containing the next line from standard input, without the trailing newline.

  • Runtime symbol: __gruel_read_line

Examples:

let line = @read_line();

String Parsing

@parse_i32

@parse_i32(s) parses s as a signed 32-bit integer. Panics on invalid input.

  • Runtime symbol: __gruel_parse_i32

Examples:

let n: i32 = @parse_i32(line);

@parse_i64

@parse_i64(s) parses s as a signed 64-bit integer. Panics on invalid input.

  • Runtime symbol: __gruel_parse_i64

Examples:

let n: i64 = @parse_i64(line);

@parse_u32

@parse_u32(s) parses s as an unsigned 32-bit integer. Panics on invalid input.

  • Runtime symbol: __gruel_parse_u32

Examples:

let n: u32 = @parse_u32(line);

@parse_u64

@parse_u64(s) parses s as an unsigned 64-bit integer. Panics on invalid input.

  • Runtime symbol: __gruel_parse_u64

Examples:

let n: u64 = @parse_u64(line);

Random Numbers

@random_u32

@random_u32() returns a uniformly distributed u32 from the runtime PRNG.

  • Runtime symbol: __gruel_random_u32

Examples:

let r = @random_u32();

@random_u64

@random_u64() returns a uniformly distributed u64 from the runtime PRNG.

  • Runtime symbol: __gruel_random_u64

Examples:

let r = @random_u64();

Compile-time Reflection

@compile_error

@compile_error("msg") aborts compilation with the given message. Useful for unreachable comptime branches.

Examples:

@compile_error("unsupported target")

@size_of

@size_of(T) returns sizeof(T) as i32, evaluated at compile time.

Examples:

@size_of(i64)

@align_of

@align_of(T) returns the required alignment of T as i32, evaluated at compile time.

Examples:

@align_of(i64)

@type_name

@type_name(T) returns the canonical name of T as a comptime-known string.

Examples:

@type_name(i64) // "i64"

@type_info

@type_info(T) returns a comptime struct describing T (kind, fields, variants, ...).

Examples:

@type_info(MyStruct)

@field

@field(value, "name") reads the named field of value, with the name resolved at compile time.

Examples:

@field(s, "x")

@import

@import("path") — planned module-system hook; currently accepted by the compiler as a placeholder.

Examples:

@import("utils.gruel")

Target Platform

@target_arch

@target_arch() returns a variant of the built-in Arch enum.

Examples:

if @target_arch() == Arch::Aarch64 { ... }

@target_os

@target_os() returns a variant of the built-in Os enum.

Examples:

if @target_os() == Os::Linux { ... }

Raw Pointers

@ptr_read

@ptr_read(p) dereferences p: ptr const T or ptr mut T and returns T. Requires an unchecked block.

  • Requires: unchecked { ... } block

Examples:

unchecked { let v = @ptr_read(p); }

@ptr_write

@ptr_write(p, v) writes v through p: ptr mut T. Requires an unchecked block.

  • Requires: unchecked { ... } block

Examples:

unchecked { @ptr_write(p, 42); }

@ptr_offset

@ptr_offset(p, n) advances p by n * sizeof(*p) bytes. Requires an unchecked block.

  • Requires: unchecked { ... } block

Examples:

unchecked { let q = @ptr_offset(p, 3); }

@ptr_to_int

@ptr_to_int(p) returns the address of p as u64. Requires an unchecked block.

  • Requires: unchecked { ... } block

Examples:

unchecked { let a = @ptr_to_int(p); }

@int_to_ptr

@int_to_ptr(addr) reinterprets addr: u64 as a pointer. Target pointer type is inferred from context. Requires an unchecked block.

  • Requires: unchecked { ... } block

Examples:

unchecked { let p: ptr mut u8 = @int_to_ptr(addr); }

@null_ptr

@null_ptr() returns a pointer whose address is zero; the pointer type is inferred from context. Requires an unchecked block.

  • Requires: unchecked { ... } block

Examples:

unchecked { let p: ptr const u8 = @null_ptr(); }

@is_null

@is_null(p) returns true iff p's address is zero. Requires an unchecked block.

  • Requires: unchecked { ... } block

Examples:

unchecked { if @is_null(p) { ... } }

@ptr_copy

@ptr_copy(dst, src, n) copies n elements of the pointee type from src to dst via LLVM memcpy. Requires an unchecked block.

  • Requires: unchecked { ... } block

Examples:

unchecked { @ptr_copy(dst, src, 16); }

@raw

@raw(place) returns ptr const T pointing to place. Requires an unchecked block.

  • Requires: unchecked { ... } block

Examples:

unchecked { let p = @raw(x); }

@raw_mut

@raw_mut(place) returns ptr mut T pointing to place. Requires an unchecked block.

  • Requires: unchecked { ... } block

Examples:

unchecked { let p = @raw_mut(x); }

System Calls

@syscall

@syscall(num, arg1, ...) issues a raw syscall. Takes the syscall number plus up to 6 arguments; returns i64. Requires an unchecked block.

  • Requires: unchecked { ... } block

Examples:

unchecked { let ret = @syscall(1, 1, buf, n); }

Iteration

@range

@range(end), @range(start, end), or @range(start, end, step) produces an iterable over integers.

Examples:

for i in @range(0, 10) { ... }

Preview / Meta

@test_preview_gate

@test_preview_gate() exists solely to verify that the preview-feature gating mechanism works. Always gated behind --preview test_infra.

  • Preview gate: --preview test_infra (ADR-0005)