Module arithmetic

Module arithmetic 

Source
Expand description

Arithmetic operations for Seq

These functions are exported with C ABI for LLVM codegen to call.

§Safety Contract

IMPORTANT: These functions are designed to be called ONLY by compiler-generated code, not by end users or arbitrary C code. The compiler’s type checker is responsible for:

  • Ensuring stack has correct number of values
  • Ensuring values are the correct types (Int for arithmetic, Int for comparisons)
  • Preventing division by zero at compile time when possible

§Overflow Behavior

All arithmetic operations use wrapping semantics for predictable, defined behavior:

  • add: i64::MAX + 1 wraps to i64::MIN
  • subtract: i64::MIN - 1 wraps to i64::MAX
  • multiply: overflow wraps around
  • divide: i64::MIN / -1 wraps to i64::MIN (special case)

This matches the behavior of Forth and Factor, providing consistency for low-level code.

Re-exports§

pub use patch_seq_add as add;
pub use patch_seq_and as and;
pub use patch_seq_band as band;
pub use patch_seq_bnot as bnot;
pub use patch_seq_bor as bor;
pub use patch_seq_bxor as bxor;
pub use patch_seq_clz as clz;
pub use patch_seq_ctz as ctz;
pub use patch_seq_divide as divide;
pub use patch_seq_eq as eq;
pub use patch_seq_gt as gt;
pub use patch_seq_gte as gte;
pub use patch_seq_int_bits as int_bits;
pub use patch_seq_lt as lt;
pub use patch_seq_lte as lte;
pub use patch_seq_multiply as multiply;
pub use patch_seq_neq as neq;
pub use patch_seq_not as not;
pub use patch_seq_or as or;
pub use patch_seq_popcount as popcount;
pub use patch_seq_push_bool as push_bool;
pub use patch_seq_push_int as push_int;
pub use patch_seq_shl as shl;
pub use patch_seq_shr as shr;
pub use patch_seq_subtract as subtract;

Functions§

patch_seq_add
Add two integers
patch_seq_and
Logical AND operation (Forth-style: multiply for boolean values)
patch_seq_band
Bitwise AND
patch_seq_bnot
Bitwise NOT (one’s complement)
patch_seq_bor
Bitwise OR
patch_seq_bxor
Bitwise XOR
patch_seq_clz
Count leading zeros
patch_seq_ctz
Count trailing zeros
patch_seq_divide
Divide two integers (a / b)
patch_seq_eq
Integer equality: =
patch_seq_gt
Greater than: >
patch_seq_gte
Greater than or equal: >=
patch_seq_int_bits
Push the bit width of Int (64)
patch_seq_lt
Less than: <
patch_seq_lte
Less than or equal: <=
patch_seq_multiply
Multiply two integers
patch_seq_neq
Not equal: <>
patch_seq_not
Logical NOT operation
patch_seq_or
Logical OR operation (Forth-style)
patch_seq_peek_int_value
Helper for peeking at the top integer value without popping
patch_seq_pop_stack
Helper for popping without extracting the value (for conditionals)
patch_seq_popcount
Population count (count number of 1 bits)
patch_seq_push_bool
Push a boolean literal onto the stack (for compiler-generated code)
patch_seq_push_int
Push an integer literal onto the stack (for compiler-generated code)
patch_seq_shl
Shift left
patch_seq_shr
Logical shift right (zero-fill)
patch_seq_subtract
Subtract two integers (a - b)