php: integers, bits and UInt64 under PHP (slice 4A of #64) #804

Closed
opened 2026-11-14 21:45:39 +00:00 by orion · 4 comments
Collaborator

Context

Step 5 of #64 (backend: PHP), slice 4 of docs/design/PHP.md section 6, part A: the integer kernel. Steps 1 to 4 (#702, #727, #735, #761) are on main: compiler/src/backend/php/{emit,body,type,natives,witness,count,writer}.trb, runtime/php/core.php, tools/conformance.sh --php, tests/conformance/php.allow, tools/php-output.sh, CI job conformance-php (5.6.40, 7.4.33, 8.4). Today body.trb reports "UInt64 arithmetic", "a UInt64 constant", "a comparison of floats or UInt64" and "a conversion to a float or UInt64" as findings, and natives.trb maps only the show natives and torb_process_exit. This issue makes every integer operation work, which is the sibling of the JavaScript slice 4B (#724) restricted to integers.

Source

  • docs/design/PHP.md section 2.3 ("Integers", UInt64), section 3 rows K1, K2, K4, K7 (integer to integer) and K8, section 6 row 4 and the paragraph "What slice 3 left out".
  • docs/design/JAVASCRIPT-BACKEND.md section 6 row 4B and #724 for the property-test shape; runtime/int64.c and runtime/ as the reference of what each symbol means; compiler/src/backend/javascript/natives.trb (emitsIntrinsic) for the sweep.

What

php: integers, bits and UInt64 under PHP. Extend the PHP emitter and the runtime; no native is added to the manifest.

  • Intrinsics over every width. Add, Subtract, Multiply, Divide, Remainder, Negate, the bit operations and both shifts, the wrapping pair (addedWrapping, multipliedWrapping), comparisons and Convert between integers, for Int8 to Int64 and UInt8 to UInt32, as PHP.md 2.3 and 3 specify (the is_int test, the guarded / and %, the shift-count panic of the C leg, the mask after a narrow left shift and after a logical shift of an unsigned type). Population count and the leading and trailing zeros are table-driven loops over bytes.
  • UInt64 as the signed 64-bit int of 2.3: new runtime/php/int64.php (arithmetic, wrapping family, comparison by sign-bit flip, multiplication, division, remainder, torb_shift_amount, conversion to text and to integer types on 32-bit halves), written in front of a program only if the program reaches it; the "UInt64 arithmetic", "UInt64 constant" and UInt64 comparison/conversion findings go away. Conversion to a float stays a finding named for slice 5 (floats).
  • K4 and K8: === and the orderings on integers, characters, booleans and isSame on objects (already ===); build constants as literals (OperatingSystem.current, Architecture.current, ByteOrder.current, the page size).
  • Intrinsic sweep. natives.trb gains an emitsIntrinsic like the JavaScript one, so each Intrinsic over each width is emitted or named as deferred (floats: slice 5, LazyForce: stays a finding).
  • Every float operation, pack/unpack, lists, maps, sets, Shown values, text natives beyond slice 1 and the whole manifest walk stay as on main.

Decided by the loop

  • Split of slice 4. Row 4 of PHP.md bundles integers, UInt64, storage and the manifest walk; that is not one reviewable PR (the JavaScript slice 4 was cut into 4A and 4B for the same reason). Part A (this issue) is the integer kernel with int64.php; part B (storage of section 4: list, buffer and text natives, the shared walk of nativeEntries() with an explicit deferred list, the memory figures of section 4, MakeUnique check) is filed as the next step when A is merged, just in time as LOOP.md asks. The #64 step 5 line says so.
  • Vector tests run against vectors generated by the C runtime in forge CI (bc is not assumed): tools/php-int-vectors.sh builds a small C generator with the runner's compiler, writes random and boundary operands for each operator and width, and compares with the PHP helpers on all three interpreters. Case count: 10^6 per operator and width is the record's figure; the implementer measures the job time in the first CI run and lowers it to a size under 10 minutes only with the figure written into PHP.md (a measurement decides, no owner question).
  • Seed. No manifest change, so no two-commit rule. PHP is not one of the seed's targets; the seed refresh stays Torben's.
  • No owner question: all of it is a technical choice with no outward effect (recommendation test).

Files

  • compiler/src/backend/php/{body,natives,emit,type,writer}.trb; new runtime/php/int64.php; runtime/php/core.php only for shared helpers.
  • tools/php-int-vectors.sh (and a small C generator under tools/), .forgejo/workflows/gates.yml for the call in conformance-php, tools/php-output.sh (a program without UInt64 has no int64.php text in its output; php_first.trb output unchanged).
  • tests/conformance/php.allow, new tests/conformance/php-integers.trb with .expected, .stderr, .exit (every width, overflow panics, UInt64 boundaries, shifts, wrapping, conversions), tests/php-findings/ updated (UInt64 case removed, float case stays).
  • docs/design/PHP.md: row 4 split into 4A done and 4B open, a "What slice 4A left out" paragraph; docs regenerated as torb docs requires.

Tests

  1. The vector test reports zero differing results on 5.6.40, 7.4.33 and 8.4 for each operator and width.
  2. Existing programs that use only integers, bits, characters, booleans and text of slice 1 and now build, run on the three interpreters and match the .expected, .stderr, .exit of the C leg join php.allow; the PR names each candidate left off (list, map, Show, float, task) with the finding.
  3. php-integers.trb passes on the C, VM, JavaScript and PHP legs (JavaScript only if it already passes there; otherwise the PR says why).
  4. tools/php-output.sh: no output contains &$, = & or __destruct; a program with no UInt64 carries no int64.php; float arithmetic still fails the build with a named finding and no file.

Acceptance criteria

  • Vector test (C runtime as oracle) shows zero differences for every integer operator and width including UInt64, on 5.6.40, 7.4.33 and 8.4, in the conformance-php job
  • UInt64 arithmetic, constants, comparisons and conversions to integer types build and run; conversion to a float still fails with a finding that names slice 5
  • php.allow grows by the programs listed in the PR; none is removed; they pass with the same expected files as the other legs
  • php_first.trb output unchanged, a program without UInt64 has no int64.php code, and the JavaScript leg is unchanged (--js with the same js.allow)
  • Row 4 of docs/design/PHP.md split (4A done, 4B open), "What slice 4A left out" written; torb format --check and torb docs check green
  • tools/check-commits.sh clean; sh tools/gates.sh a and the conformance-php job green in forge CI (CI only, no local bootstrap or gates)

Seed

Compiler, runtime and tools only: no syntax change, no new native, no new std name looked up by string, no new Architecture case. Not touched: seed/, releases.

Size

One PR, one or two Conventional Commits (feat(php): ..., test(php): ...). Model: sonnet, thinking high (compiler work). Out of scope: part B (storage, manifest walk, memory figures), slices 5 to 8, a minifier.

Dependencies

  • #761 (closed, PR #766 merged on main).

Part of #64 (epic; do not close it).

## Context Step 5 of #64 (backend: PHP), slice 4 of `docs/design/PHP.md` section 6, **part A**: the integer kernel. Steps 1 to 4 (#702, #727, #735, #761) are on `main`: `compiler/src/backend/php/{emit,body,type,natives,witness,count,writer}.trb`, `runtime/php/core.php`, `tools/conformance.sh --php`, `tests/conformance/php.allow`, `tools/php-output.sh`, CI job `conformance-php` (5.6.40, 7.4.33, 8.4). Today `body.trb` reports "UInt64 arithmetic", "a UInt64 constant", "a comparison of floats or UInt64" and "a conversion to a float or UInt64" as findings, and `natives.trb` maps only the show natives and `torb_process_exit`. This issue makes every integer operation work, which is the sibling of the JavaScript slice 4B (#724) restricted to integers. ## Source - `docs/design/PHP.md` section 2.3 ("Integers", `UInt64`), section 3 rows K1, K2, K4, K7 (integer to integer) and K8, section 6 row 4 and the paragraph "What slice 3 left out". - `docs/design/JAVASCRIPT-BACKEND.md` section 6 row 4B and #724 for the property-test shape; `runtime/int64.c` and `runtime/` as the reference of what each symbol means; `compiler/src/backend/javascript/natives.trb` (`emitsIntrinsic`) for the sweep. ## What *php: integers, bits and UInt64 under PHP.* Extend the PHP emitter and the runtime; no native is added to the manifest. - **Intrinsics over every width.** `Add`, `Subtract`, `Multiply`, `Divide`, `Remainder`, `Negate`, the bit operations and both shifts, the wrapping pair (`addedWrapping`, `multipliedWrapping`), comparisons and `Convert` between integers, for `Int8` to `Int64` and `UInt8` to `UInt32`, as PHP.md 2.3 and 3 specify (the `is_int` test, the guarded `/` and `%`, the shift-count panic of the C leg, the mask after a narrow left shift and after a logical shift of an unsigned type). Population count and the leading and trailing zeros are table-driven loops over bytes. - **`UInt64`** as the signed 64-bit `int` of 2.3: new `runtime/php/int64.php` (arithmetic, wrapping family, comparison by sign-bit flip, multiplication, division, remainder, `torb_shift_amount`, conversion to text and to integer types on 32-bit halves), written in front of a program **only if the program reaches it**; the "UInt64 arithmetic", "UInt64 constant" and UInt64 comparison/conversion findings go away. Conversion **to a float** stays a finding named for slice 5 (floats). - **K4 and K8**: `===` and the orderings on integers, characters, booleans and `isSame` on objects (already `===`); build constants as literals (`OperatingSystem.current`, `Architecture.current`, `ByteOrder.current`, the page size). - **`Intrinsic` sweep.** `natives.trb` gains an `emitsIntrinsic` like the JavaScript one, so each `Intrinsic` over each width is emitted or named as deferred (floats: slice 5, `LazyForce`: stays a finding). - Every float operation, `pack`/`unpack`, lists, maps, sets, `Show`n values, text natives beyond slice 1 and the whole manifest walk stay as on `main`. ## Decided by the loop - **Split of slice 4.** Row 4 of PHP.md bundles integers, `UInt64`, storage and the manifest walk; that is not one reviewable PR (the JavaScript slice 4 was cut into 4A and 4B for the same reason). Part A (this issue) is the integer kernel with `int64.php`; **part B** (storage of section 4: list, buffer and text natives, the shared walk of `nativeEntries()` with an explicit deferred list, the memory figures of section 4, `MakeUnique` check) is filed as the next step when A is merged, just in time as LOOP.md asks. The #64 step 5 line says so. - **Vector tests** run against vectors generated by the C runtime in forge CI (`bc` is not assumed): `tools/php-int-vectors.sh` builds a small C generator with the runner's compiler, writes random and boundary operands for each operator and width, and compares with the PHP helpers on all three interpreters. Case count: 10^6 per operator and width is the record's figure; the implementer measures the job time in the first CI run and lowers it to a size under 10 minutes only with the figure written into PHP.md (a measurement decides, no owner question). - **Seed.** No manifest change, so no two-commit rule. PHP is not one of the seed's targets; the seed refresh stays Torben's. - No owner question: all of it is a technical choice with no outward effect (recommendation test). ## Files - `compiler/src/backend/php/{body,natives,emit,type,writer}.trb`; new `runtime/php/int64.php`; `runtime/php/core.php` only for shared helpers. - `tools/php-int-vectors.sh` (and a small C generator under `tools/`), `.forgejo/workflows/gates.yml` for the call in `conformance-php`, `tools/php-output.sh` (a program without `UInt64` has no `int64.php` text in its output; `php_first.trb` output unchanged). - `tests/conformance/php.allow`, new `tests/conformance/php-integers.trb` with `.expected`, `.stderr`, `.exit` (every width, overflow panics, `UInt64` boundaries, shifts, wrapping, conversions), `tests/php-findings/` updated (UInt64 case removed, float case stays). - `docs/design/PHP.md`: row 4 split into 4A done and 4B open, a "What slice 4A left out" paragraph; docs regenerated as `torb docs` requires. ## Tests 1. The vector test reports zero differing results on 5.6.40, 7.4.33 and 8.4 for each operator and width. 2. Existing programs that use only integers, bits, characters, booleans and text of slice 1 and now build, run on the three interpreters and match the `.expected`, `.stderr`, `.exit` of the C leg join `php.allow`; the PR names each candidate left off (list, map, `Show`, float, task) with the finding. 3. `php-integers.trb` passes on the C, VM, JavaScript and PHP legs (JavaScript only if it already passes there; otherwise the PR says why). 4. `tools/php-output.sh`: no output contains `&$`, `= &` or `__destruct`; a program with no `UInt64` carries no `int64.php`; float arithmetic still fails the build with a named finding and no file. ## Acceptance criteria - [ ] Vector test (C runtime as oracle) shows zero differences for every integer operator and width including `UInt64`, on 5.6.40, 7.4.33 and 8.4, in the `conformance-php` job - [ ] `UInt64` arithmetic, constants, comparisons and conversions to integer types build and run; conversion to a float still fails with a finding that names slice 5 - [ ] `php.allow` grows by the programs listed in the PR; none is removed; they pass with the same expected files as the other legs - [ ] `php_first.trb` output unchanged, a program without `UInt64` has no `int64.php` code, and the JavaScript leg is unchanged (`--js` with the same `js.allow`) - [ ] Row 4 of `docs/design/PHP.md` split (4A done, 4B open), "What slice 4A left out" written; `torb format --check` and `torb docs check` green - [ ] `tools/check-commits.sh` clean; `sh tools/gates.sh a` and the `conformance-php` job green in forge CI (CI only, no local bootstrap or gates) ## Seed Compiler, runtime and tools only: no syntax change, no new native, no new std name looked up by string, no new `Architecture` case. Not touched: `seed/`, releases. ## Size One PR, one or two Conventional Commits (`feat(php): ...`, `test(php): ...`). Model: sonnet, thinking high (compiler work). Out of scope: part B (storage, manifest walk, memory figures), slices 5 to 8, a minifier. ## Dependencies - #761 (closed, PR #766 merged on `main`). Part of #64 (epic; do not close it).
Author
Collaborator

[loop:torbscript] claim implement by session:loop-torbscript-804-impl until 2026-11-15T09:35Z

[loop:torbscript] claim implement by session:loop-torbscript-804-impl until 2026-11-15T09:35Z
Author
Collaborator

[loop:torbscript] release implement: PR #809 open, CI queued (runs 3009/3010 waiting behind 2997/2998/3003 on the busy runner, not started after ~15 min; nothing has run yet, so the build is unverified and the conformance-php vector timing is not yet recorded in PHP.md), worker usage about 600k tokens (sonnet). next: waiting (merge gate)

[loop:torbscript] release implement: PR https://git.torb.dev/torbscript/language/pulls/809 open, CI queued (runs 3009/3010 waiting behind 2997/2998/3003 on the busy runner, not started after ~15 min; nothing has run yet, so the build is unverified and the conformance-php vector timing is not yet recorded in PHP.md), worker usage about 600k tokens (sonnet). next: waiting (merge gate)
Author
Collaborator

[loop:torbscript] claim review-fix by session:loop-torbscript-804-fix until 2026-11-15T06:06Z

[loop:torbscript] claim review-fix by session:loop-torbscript-804-fix until 2026-11-15T06:06Z
Author
Collaborator

[loop:torbscript] release review-fix by session:loop-torbscript-804-fix: bootstrap (linux-x64) failed because the seed has no command call inside an expression; 23 errors, all if isWide shape {, isFloating kind, needsWide symbol in compiler/src/backend/php/body.trb and Some kind in compiler/tests/php-natives.test.trb. Fixed with parentheses in commit 4e48f8b3 (11 lines, nothing else touched). Not run locally (no runnable torb on the worker), CI on 4e48f8b3 is queued (runs 2979/2980 waiting for a runner), not yet green. next: review (merge gate)

[loop:torbscript] release review-fix by session:loop-torbscript-804-fix: bootstrap (linux-x64) failed because the seed has no command call inside an expression; 23 errors, all `if isWide shape {`, `isFloating kind`, `needsWide symbol` in compiler/src/backend/php/body.trb and `Some kind` in compiler/tests/php-natives.test.trb. Fixed with parentheses in commit 4e48f8b3 (11 lines, nothing else touched). Not run locally (no runnable torb on the worker), CI on 4e48f8b3 is queued (runs 2979/2980 waiting for a runner), not yet green. next: review (merge gate)
argus closed this issue 2026-11-15 11:31:05 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
torbscript/language#804
No description provided.