Files
Sylpheed/tools/ppc-manual/branch/tw.md
sim 84856fc081
All checks were successful
CI / Native — linux (pull_request) Successful in 2h1m57s
CI / WASM — Web (pull_request) Successful in 28m8s
CI / Formatting (pull_request) Successful in 1m26s
docs(ppc-manual): check every xenia-rs claim against Canary's source
The hand-written parts of the manual still described how the retired
xenia-rs interpreter behaved: its snapshots, Rust casts and helpers. Each of
those 490 statements is now either restated as what Canary's emitters and
x64 backend actually do (at the pinned canary_experimental commit), or
dropped where it only made sense for xenia-rs.

Checking them turned up claims that were wrong, not just outdated:

- VSCR[SAT] is never modelled in Canary (DID_SATURATE is a stub and mfvscr
  cannot see it); the pages said saturating ops set it stickily.
- Canary does not implement lswi/lswx/stswi/stswx, dcbi, mtfsb0/mtfsb1,
  vmsum*, vmhaddshs, vupkhpx/vupklpx, and most SPRs; pages described them
  as working.
- Traps evaluate TO in Canary; stvebx/stvehx/stvewx store one element, not
  16 bytes; mtmsrd writes only EE; fres/frsqrte/vrsqrtefp precision claims
  and the stfs "rounds under RN / sets FPSCR" claim contradicted the spec.
- Reservations are a 64 KiB block bitmap plus a value compare, not
  per-address tracking.

Claims that neither Canary's source nor a public spec settles are marked
unverified (NI at boot, vmaddcfp128 operand order, estimate bit-exactness).

Generated regions are untouched; re-running the generator changes nothing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-16 21:52:38 +02:00

7.0 KiB
Raw Blame History

tw — Trap Word

Category: Branch & System · Form: X · Opcode: 0x7c000008

Assembler Mnemonics

Mnemonic XML entry Flags Description
tw tw — Trap Word

Syntax

tw [TO], [RA], [RB]

Encoding

tw — form X

  • Opcode word: 0x7c000008
  • Primary opcode (bits 0–5): 31
  • Extended opcode: 4
  • Synchronising: no
Bits Field Meaning
0–5 OPCD primary opcode
6–10 RT/FRT/VRT destination
11–15 RA/FRA/VRA source A
16–20 RB/FRB/VRB source B
21–30 XO extended opcode (10 bits)
31 Rc record-form flag

Operands

Field Role Description
TO tw: read Trap-on condition mask (5 bits) — LT, GT, EQ, LGT, LLT bits.
RA tw: read Source GPR (r0–r31).
RB tw: read Source GPR.

Register Effects

tw

  • Reads (always): TO, RA, RB
  • Reads (conditional): none
  • Writes (always): none
  • Writes (conditional): none

Status-Register Effects

No condition-register or status-register effects.

Operation (pseudocode)

; No hand-written pseudocode for this instruction yet.
; The authoritative semantics are the Canary emitter snapshot under
; Implementation References; about half of Canary's emitters open
; with the PPC-style definition as a comment (`RD <- (RA) + (RB)`).
; Every side effect is also enumerated in the Register Effects and
; Status-Register Effects tables above.

C Translation Example

/* No hand-written C yet. Translate the Canary emitter snapshot   */
/* under Implementation References; its HIR maps directly:        */
/*   f.LoadGPR(n) / f.StoreGPR(n, v)  -> r[n] / r[n] = v          */
/*   f.LoadFPR / StoreFPR, f.LoadVR / StoreVR -> f[n], v[n]        */
/*   f.Load(ea, T), f.Store(ea, v) -> raw read / write; emitters   */
/*     wrap them in f.ByteSwap for the big-endian guest value      */
/*   f.UpdateCR(n, v)  -> CR field n from v's LOW 32 BITS vs 0     */
/*   f.LoadCA / f.StoreCA -> xer.CA;  f.StoreSAT -> vscr.SAT       */
/*   i.XO.RA, i.D.DS, ... -> the bit-fields listed under Operands  */
/* The Register Effects and Status-Register Effects tables above  */
/* enumerate every side effect a faithful translation must emit.  */

Implementation References

tw

Canary emitter (frozen snapshot @ f21ebd49e9)
int InstrEmit_tw(PPCHIRBuilder& f, const InstrData& i) {
  if (cvars::ignore_trap_instructions) {
    return 0;
  }
  // a <- EXTS((RA)[32:63])
  // b <- EXTS((RB)[32:63])
  // if (a < b) & TO[0] then TRAP
  // if (a > b) & TO[1] then TRAP
  // if (a = b) & TO[2] then TRAP
  // if (a <u b) & TO[3] then TRAP
  // if (a >u b) & TO[4] then TRAP
  Value* ra =
      f.SignExtend(f.Truncate(f.LoadGPR(i.X.RA), INT32_TYPE), INT64_TYPE);
  Value* rb =
      f.SignExtend(f.Truncate(f.LoadGPR(i.X.RB), INT32_TYPE), INT64_TYPE);
  return InstrEmit_trap(f, i, ra, rb, i.X.RT);
}

Special Cases & Edge Conditions

  • 32-bit comparison only. tw compares the low 32 bits of RA and RB. The high halves of the 64-bit GPRs on the Xenon are ignored. Use td for full 64-bit comparisons.

  • TO mask (5 bits, MSB-first). Same encoding as the doubleword form:

    Bit Mnemonic Triggered when
    TO[0] (16) LT (int32) RA < (int32) RB
    TO[1] (8) GT (int32) RA > (int32) RB
    TO[2] (4) EQ (uint32) RA == (uint32) RB
    TO[3] (2) LGT (uint32) RA < (uint32) RB
    TO[4] (1) LLT (uint32) RA > (uint32) RB
  • tw 31, 0, 0 is trap. The simplified mnemonic trap expands to tw 31, r0, r0 — all five TO bits set ⇒ unconditional trap. Compilers and the kernel use this as the assertion / debugger break primitive; it appears as 0x7FE00008 in raw bytes.

  • Conditional asserts. GCC's __builtin_trap and MSVC's __assert macros emit tw variants like twge/twlt to fault on bound-check failures.

  • No register effects. Side effect only: Program interrupt (0x700) with SRR1[TRAP]=1.

  • Canary evaluates the condition. Canary evaluates TO properly: it compares the operands for each set bit and emits a conditional trap (TrapTrue), and TO = 0 emits nothing. tw compares the sign-extended low 32 bits of both operands, and inert markers like tw 0, r0, r0 fall through.

  • Inert encoding. tw 0, r0, r0 (no TO bits set) can never trap on real hardware, and Canary emits nothing for TO = 0. It encodes as 0x7C000008 — sometimes used as a structured-NOP marker.

  • twi — same 32-bit comparison against a 16-bit signed immediate.
  • td / tdi — 64-bit (doubleword) variants.
  • sc — kernel entry via system-call exception (different vector).
  • mtmsr — kernel returns from 0x700 via rfid.

Simplified Mnemonics

Simplified Expansion Triggered when
trap tw 31, 0, 0 unconditional
tweq RA, RB tw 4, RA, RB RA == RB
twne RA, RB tw 24, RA, RB RA != RB
twlt RA, RB tw 16, RA, RB signed less than
twle RA, RB tw 20, RA, RB signed less or equal
twgt RA, RB tw 8, RA, RB signed greater than
twge RA, RB tw 12, RA, RB signed greater or equal
twllt RA, RB tw 2, RA, RB unsigned less than
twlge RA, RB tw 5, RA, RB unsigned greater or equal
twlgt RA, RB tw 1, RA, RB unsigned greater than
twlle RA, RB tw 6, RA, RB unsigned less or equal

IBM Reference