Files
Sylpheed/tools/ppc-manual/branch/twi.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

6.9 KiB
Raw Blame History

twi — Trap Word Immediate

Category: Branch & System · Form: D · Opcode: 0x0c000000

Assembler Mnemonics

Mnemonic XML entry Flags Description
twi twi — Trap Word Immediate

Syntax

tw [TO], [RA], [SIMM]

Encoding

twi — form D

  • Opcode word: 0x0c000000
  • Primary opcode (bits 0–5): 3
  • Extended opcode: —
  • Synchronising: no
Bits Field Meaning
0–5 OPCD primary opcode
6–10 RT destination GPR (or RS when storing)
11–15 RA source GPR (0 ⇒ literal 0 for RA0 forms)
16–31 D/SI/UI 16-bit signed or unsigned immediate

Operands

Field Role Description
TO twi: read Trap-on condition mask (5 bits) — LT, GT, EQ, LGT, LLT bits.
RA twi: read Source GPR (r0–r31).
SIMM twi: read 16-bit signed immediate. Sign-extended to 64 bits before use.

Register Effects

twi

  • Reads (always): TO, RA, SIMM
  • 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

twi

Canary emitter (frozen snapshot @ f21ebd49e9)
int InstrEmit_twi(PPCHIRBuilder& f, const InstrData& i) {
  if (cvars::ignore_trap_instructions) {
    return 0;
  }
  // a <- EXTS((RA)[32:63])
  // if (a < EXTS(SI)) & TO[0] then TRAP
  // if (a > EXTS(SI)) & TO[1] then TRAP
  // if (a = EXTS(SI)) & TO[2] then TRAP
  // if (a <u EXTS(SI)) & TO[3] then TRAP
  // if (a >u EXTS(SI)) & TO[4] then TRAP
  if (i.D.RA == 0 && i.D.RT == 0x1F) {
    // This is a special trap. Probably.
    uint16_t type = (uint16_t)XEEXTS16(i.D.DS);
    f.Trap(type);
    return 0;
  }
  Value* ra =
      f.SignExtend(f.Truncate(f.LoadGPR(i.D.RA), INT32_TYPE), INT64_TYPE);
  Value* rb = f.LoadConstantInt64(XEEXTS16(i.D.DS));
  return InstrEmit_trap(f, i, ra, rb, i.D.RT);
}

Special Cases & Edge Conditions

  • 32-bit comparison against sign-extended immediate. SIMM is a 16-bit signed value, sign-extended to 32 bits, then compared against the low 32 bits of RA. The high half of RA is ignored.
  • TO mask. Identical to tw: bit 0 = signed LT, 1 = signed GT, 2 = EQ, 3 = unsigned LT (LGT), 4 = unsigned GT (LLT). Trap fires if any selected bit's condition is true.
  • twi 31, 0, 0 is unconditional trap. All TO bits set ⇒ guaranteed trap. The simplified mnemonic family (twnei, twgei, …) is much more common in real code: bound checks, null checks, integer-divide-by-zero pre-checks.
  • Compiler usage. Xbox 360 GCC emits twnei rN, -1 and similar to validate handle-style return values; the kernel handler turns the trap into an exception delivered to the title.
  • No register effects. Side effect: Program interrupt → vector 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. twi 0, r0, 0 therefore falls through. One special case: twi 31, r0, <imm> (TO = 31, RA = 0) becomes an unconditional Trap carrying <imm> as its type.
  • No Rc / OE. D-form trap immediates have neither.
  • tw — register-register 32-bit trap (X-form).
  • tdi / td — 64-bit (doubleword) siblings.
  • sc — alternative synchronous kernel entry.
  • cmpi, cmpli — set CR for a subsequent bcx when you want a regular branch instead of a trap.

Simplified Mnemonics

Simplified Expansion Triggered when
tweqi RA, value twi 4, RA, value RA == EXTS(value)
twnei RA, value twi 24, RA, value RA != EXTS(value)
twlti RA, value twi 16, RA, value signed less than
twlei RA, value twi 20, RA, value signed less or equal
twgti RA, value twi 8, RA, value signed greater than
twgei RA, value twi 12, RA, value signed greater or equal
twllti RA, value twi 2, RA, value unsigned less than
twlgei RA, value twi 5, RA, value unsigned greater or equal
twlgti RA, value twi 1, RA, value unsigned greater than
twllei RA, value twi 6, RA, value unsigned less or equal

IBM Reference