Files
Sylpheed/tools/ppc-manual/memory/ldarx.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.6 KiB
Raw Blame History

ldarx — Load Doubleword and Reserve Indexed

Category: Memory · Form: X · Opcode: 0x7c0000a8

Assembler Mnemonics

Mnemonic XML entry Flags Description
ldarx ldarx — Load Doubleword and Reserve Indexed

Syntax

ldarx [RD], [RA0], [RB]

Encoding

ldarx — form X

  • Opcode word: 0x7c0000a8
  • Primary opcode (bits 0–5): 31
  • Extended opcode: 84
  • 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
RA0 ldarx: read Source GPR; when the encoded register number is 0 the operand is the literal 64-bit zero, not r0.
RB ldarx: read Source GPR.
RD ldarx: write Destination GPR.

Register Effects

ldarx

  • Reads (always): RA0, RB
  • Reads (conditional): none
  • Writes (always): RD
  • 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

ldarx

Canary emitter (frozen snapshot @ f21ebd49e9)
int InstrEmit_ldarx(PPCHIRBuilder& f, const InstrData& i) {
  // if RA = 0 then
  //   b <- 0
  // else
  //   b <- (RA)
  // EA <- b + (RB)
  // RESERVE <- 1
  // RESERVE_LENGTH <- 8
  // RESERVE_ADDR <- real_addr(EA)
  // RT <- MEM(EA, 8)

  // NOTE: we assume we are within a global lock.
  // We could assert here that the block (or its parent) has taken a global lock
  // already, but I haven't see anything but interrupt callbacks (which are
  // always under a global lock) do that yet.
  // We issue a memory barrier here to make sure that we get good values.
  Value* ea = CalculateEA_0(f, i.X.RA, i.X.RB);

  if (cvars::no_reserved_ops) {
    f.StoreGPR(i.X.RT, f.ByteSwap(f.Load(ea, INT64_TYPE)));

  } else {
    f.MemoryBarrier();

    Value* rt = f.ByteSwap(f.LoadWithReserve(ea, INT64_TYPE));
    f.StoreGPR(i.X.RT, rt);
  }
  return 0;
}

Special Cases & Edge Conditions

  • Reservation set. Loads the doubleword at EA and atomically establishes a reservation on that address. A subsequent stdcx at the same address completes only if the reservation is still valid. Together they form a standard load-linked / store-conditional pair for lock-free updates.
  • One reservation per thread. Hardware: each hardware thread holds at most one reservation at a time, and a new ldarx (or lwarx) discards the prior one. Canary instead sets a bit for the 64 KiB block holding EA in a bitmap shared by all threads and caches the loaded value per thread; taking a second reservation while one is held hits a DebugBreak (int3) in its helper.
  • Granule. Architecturally the reservation covers a single naturally-aligned doubleword (8 bytes). On Xenon the practical reservation granule is one cache line (128 bytes) — any store to that line by another agent loses the reservation. Canary's granule is a 64 KiB block, and ordinary stores never clear it: stdcx. succeeds if this thread still holds the block bit and the doubleword still holds the value ldarx read.
  • Alignment requirement. EA must be 8-byte aligned. An unaligned ldarx raises an alignment exception on hardware. Canary does not check; pass aligned addresses.
  • RA0 semantics. When RA = 0, base is literal zero — ldarx RT, 0, RB reads at exact RB. Used in synthetic-zero atomic-init idioms, but rare.
  • Reservation-loss events. Any exception, context switch, or store by another thread to the reserved line clears the reservation. Application code must treat the stdcx failure as a normal retry condition, not as an error.
  • Pair atomically. Code must be ldarx ... do work ... stdcx. with no intervening loads or stores that could be re-ordered. Optionally fence with lwsync inside the loop. The conditional store sets CR0[EQ] to report success.
  • stdcx — store-conditional doubleword (the matching half of the pair).
  • lwarx / stwcx — 32-bit reservation pair.
  • ld, ldx — non-reserving doubleword loads.
  • sync, lwsync — barriers commonly placed around reservation pairs.

IBM Reference