Repository navigation
Description
Comptime evaluation of bit-manipulation methods (§4.2.4)
Summary
The integer bit-manipulation methods required by §4.2.4 work in runtime LLVM codegen, but they are not comptime-evaluable. Constants using rotate_left, rotate_right, swap_bytes, popcount, clz, ctz, or bitreverse either fail or avoid the method path instead of producing the specified constant result.
Spec
docs/with-specification.md:1031-1145 defines bit methods and explicitly requires compile-time evaluation when operands are known at comptime. popcount, clz, and ctz return i32; clz(0) and ctz(0) return the integer width; swap_bytes is identity for 8-bit integers; bitreverse and rotates operate at the integer width.
Current state
src/MirLower.w:5852-5894classifies integer methods includingrotate_left,rotate_right,swap_bytes,popcount,clz,ctz,bitreverse,min,max, andabs.src/CodegenDispatch.w:7070-7190lowers the runtime LLVM forms: rotates from shifts/or,swap_bytesviallvm.bswapor identity for <=8 bits,popcountviallvm.ctpop,clz/ctzvia LLVM count intrinsics with zero-defined behavior, andbitreverseviallvm.bitreverse.src/ComptimeEval.whas no corresponding evaluator entries for these method names. A scan forrotate_left,rotate_right,swap_bytes,popcount,clz,ctz, andbitreversefound the methods in MIR/codegen, not in comptime evaluation.- Existing behavior tests cover some runtime cases, such as
test/behavior/behav_rotate.wandtest/behavior/behav_int_swap_bytes.w, but do not prove const/comptime evaluation.
Implementation plan
- Add a comptime integer-method dispatch path in
src/ComptimeEval.wparallel to the existing runtime intrinsic classification. Keep the method list centralized or mirrored deliberately so runtime and comptime support cannot drift silently. - Implement width-aware exact-integer operations for:
rotate_left/rotate_right, masking shift counts by width,swap_bytes, with 8-bit identity,popcount,clz,ctz, including zero returning width forclz/ctz,bitreverse.
- Return
i32comptime values forpopcount,clz, andctz; return the receiver type for rotates,swap_bytes, andbitreverse. - Reject non-integer receivers through the existing Sema path; the comptime evaluator should not accept a method that Sema would reject.
- Verify signed integer handling uses the bit pattern at the declared width, not host signed arithmetic semantics.
Test plan
- Add
test/behavior/behav_comptime_bit_methods.wwithconstorcomptimevalues for each method and representative widths (u8,u16,u32,u64, and one signed type). - Include zero cases for
clzandctz. - Include rotate counts equal to or larger than the bit width.
- Include
swap_bytesonu8and a multi-byte integer. - Include
bitreverseon a small width where the expected value is obvious. - Run
with build,with build :fixpoint, andwith build :test.
Acceptance criteria
- All §4.2.4 integer bit methods are comptime-evaluable.
- Comptime results match runtime codegen results for the same inputs.
-
popcount,clz, andctzproducei32values. - Zero handling for
clz/ctzfollows the spec. - Signed receivers are evaluated by fixed-width bit pattern, not host overflow behavior.
Dependencies
Overlaps #483, which is broader behavior-test coverage for bit methods. This issue should add the comptime-specific tests needed for the implementation.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 4.2.4.28.
Activity
ehartford commentedon Jun 12, 2026
Implemented in 298d951. Integer bit methods now evaluate in comptime for rotate_left, rotate_right, swap_bytes, popcount, clz, ctz, and bitreverse, with fixed-width bit-pattern semantics and signed receiver coverage. Added positive behavior coverage and a negative arity test. Verification passed: build, fixpoint, full test suite, and test-green. Follow-up #565 tracks the separate comptime pipeline syntax dispatch bug found during testing.