Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tempfile = "3.8"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tree-sitter = "0.25.6"
tree-sitter-tribute = { git = "https://github.com/Kroisse/tree-sitter-tribute", tag = "v0.4.1" }
tree-sitter-tribute = { git = "https://github.com/Kroisse/tree-sitter-tribute", tag = "v0.4.2" }
tribute-cranelift = { path = "crates/tribute-cranelift" }
tribute-core = { path = "crates/tribute-core" }
tribute-front = { path = "crates/tribute-front" }
Expand Down
3 changes: 3 additions & 0 deletions crates/tribute-passes/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,9 @@ impl<'db> Resolver<'db> {
"String" => *core::String::new(self.db),
"Bytes" => *core::Bytes::new(self.db),
"Nil" => *core::Nil::new(self.db),
// Low-level integer types (for runtime implementation)
"I32" => *core::I32::new(self.db),
"I64" => *core::I64::new(self.db),
_ => {
// Look up user-defined types in the environment
if let Some(binding) = self.lookup_binding(name) {
Expand Down
80 changes: 80 additions & 0 deletions lib/std/runtime/limb.trb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Limb intrinsics for arbitrary precision arithmetic.
//
// These intrinsics operate on 32-bit unsigned limbs (the building blocks
// for BigNat/BigInt). The I32 type is signless; these operations treat
// the values as unsigned.
//
// Each arithmetic operation returns a tuple of (result, carry/borrow),
// enabling efficient multi-precision arithmetic.

// =============================================================================
// Limb arithmetic intrinsics
// =============================================================================

/// Add two limbs with carry-in, returning (sum, carry-out).
///
/// Computes: sum = a + b + carry_in (mod 2^32)
/// Returns: (sum, 1 if overflow else 0)
///
/// carry_in must be 0 or 1.
extern "intrinsic" fn __limb_add(a: I32, b: I32, carry_in: I32) -> #(I32, I32)

/// Subtract two limbs with borrow-in, returning (difference, borrow-out).
///
/// Computes: diff = a - b - borrow_in (mod 2^32)
/// Returns: (diff, 1 if underflow else 0)
///
/// borrow_in must be 0 or 1.
extern "intrinsic" fn __limb_sub(a: I32, b: I32, borrow_in: I32) -> #(I32, I32)

/// Multiply two limbs, returning full 64-bit result as (low, high).
///
/// Computes: result = a * b (unsigned)
/// Returns: (low 32 bits, high 32 bits)
extern "intrinsic" fn __limb_mul(a: I32, b: I32) -> #(I32, I32)

/// Divide a 64-bit value by a 32-bit divisor, returning (quotient, remainder).
///
/// Computes: (high << 32 | low) / divisor
/// Returns: (quotient, remainder)
///
/// Precondition: high < divisor (ensures quotient fits in 32 bits)
/// Panics if divisor is 0 or if high >= divisor.
extern "intrinsic" fn __limb_div(high: I32, low: I32, divisor: I32) -> #(I32, I32)

/// Compare two limbs (unsigned).
///
/// Returns: -1 if a < b, 0 if a == b, 1 if a > b
extern "intrinsic" fn __limb_cmp(a: I32, b: I32) -> I32

// =============================================================================
// Limb module with wrapper functions
// =============================================================================

pub mod Limb {
/// Add two limbs with carry.
pub fn add(a: I32, b: I32, carry: I32) -> #(I32, I32) {
__limb_add(a, b, carry)
}

/// Subtract two limbs with borrow.
pub fn sub(a: I32, b: I32, borrow: I32) -> #(I32, I32) {
__limb_sub(a, b, borrow)
}

/// Multiply two limbs (wide multiplication).
pub fn mul(a: I32, b: I32) -> #(I32, I32) {
__limb_mul(a, b)
}

/// Divide 64-bit by 32-bit (wide division).
pub fn div(high: I32, low: I32, divisor: I32) -> #(I32, I32) {
__limb_div(high, low, divisor)
}

/// Compare two limbs (unsigned).
/// Returns -1, 0, or 1.
pub fn cmp(a: I32, b: I32) -> I32 {
__limb_cmp(a, b)
}
}
Loading