dymokomi/luce-base
Source of release 0.45.0
0.45.0 / src / back / native / x86_64 / atomics.lucb
#==============================================================================================
#
# x86_64/atomics - Atomics
#
# DESCRIPTION:
# Atomic read-modify-write operations and compare-and-swap, as the target's
# instructions or a loop that retries (ยง15).
#
#==============================================================================================
extend Generator:
# mark: Atomics ============================================================================
## Read-modify-write atomics: `xadd` and `xchg` for the ones the machine has, a
## compare-and-swap loop for the rest; the old value is the result.
func read_modify_write(n: ir.Instr) -> !:
let cls = ir.Class.w if n.extra <= 4 else ir.Class.l
let width = (u64)n.extra
let suffix = width_suffix(n.extra)
let value = register_of_width("%r11", width)
let old = register_of_width("%rax", width)
let fresh = register_of_width("%rdx", width)
try self.operand(self.reg(cls, 1), cls, n.a)
try self.address_operand("%r10", n.b)
if n.op == ir.Op.atomic_add or n.op == ir.Op.atomic_sub:
if n.op == ir.Op.atomic_sub:
try self.writer.line4("neg", suffix, " ", value)
try self.writer.line5("lock xadd", suffix, " ", value, ", (%r10)")
try self.store_temp(self.reg(cls, 1), cls, n.dst)
return
if n.op == ir.Op.atomic_swap:
try self.writer.line5("xchg", suffix, " ", value, ", (%r10)")
try self.store_temp(self.reg(cls, 1), cls, n.dst)
return
try self.writer.line4(zero_extending_load(n.extra), "(%r10), ", self.reg(cls, 0), "")
try self.writer.put("\n1:")
try self.writer.line5("mov", suffix, " ", old, ", ")
try self.writer.put(fresh)
match n.op:
.atomic_or:
try self.writer.line5("or", suffix, " ", value, ", ")
try self.writer.put(fresh)
.atomic_xor:
try self.writer.line5("xor", suffix, " ", value, ", ")
try self.writer.put(fresh)
.atomic_and_not:
try self.writer.line4("not", suffix, " ", fresh)
try self.writer.line5("or", suffix, " ", value, ", ")
try self.writer.put(fresh)
try self.writer.line4("not", suffix, " ", fresh)
.atomic_max:
try self.writer.line5("cmp", suffix, " ", value, ", ")
try self.writer.put(old)
try self.writer.line4("cmovl", suffix if cls == ir.Class.l else "l", " ", self.reg(cls, 1))
try self.writer.put(", ")
try self.writer.put(register_of_width("%rdx", 8 if cls == ir.Class.l else 4))
.atomic_min:
try self.writer.line5("cmp", suffix, " ", value, ", ")
try self.writer.put(old)
try self.writer.line4("cmovg", suffix if cls == ir.Class.l else "l", " ", self.reg(cls, 1))
try self.writer.put(", ")
try self.writer.put(register_of_width("%rdx", 8 if cls == ir.Class.l else 4))
_:
_ = fresh.length
try self.writer.line5("lock cmpxchg", suffix, " ", fresh, ", (%r10)")
try self.writer.line("jne 1b")
try self.store_temp(self.reg(cls, 0), cls, n.dst)
## `lock cmpxchg`: the expected value in rax, the desired one in r11; rax holds what
## memory held, which is the result.
func compare_and_swap(n: ir.Instr) -> !:
let cls = ir.Class.w if n.extra <= 4 else ir.Class.l
let width = (u64)n.extra
let expected = self.f.args.get((usize)n.b.value)
let desired = self.f.args.get((usize)n.b.value + 1)
try self.operand(self.reg(cls, 0), cls, expected.value)
try self.operand(self.reg(cls, 1), cls, desired.value)
try self.address_operand("%r10", n.a)
try self.writer.line5("lock cmpxchg", width_suffix(n.extra), " ", register_of_width("%r11", width), ", (%r10)")
try self.store_temp(self.reg(cls, 0), cls, n.dst)