#! @file atomics.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the code of atomics on x86-64, whose ordinary reads and writes are #! already ordered as `acquire` and `release` ask. So a load is a `mov` at every #! order, a store a `mov` but of `seq_cst`, which is an `xchg`, and a fence is #! an `mfence` of `seq_cst` and nothing otherwise. An exchange is an `xchg`, #! which locks by itself, a compare-exchange a `lock cmpxchg`, adding and #! subtracting a `lock xadd`, and the rest a loop of `lock cmpxchg`, every one of #! which orders as `seq_cst` does. An order not known while compiling is tested #! where it matters, which costs a compare. #! #! They work in `rax`, `rcx`, and `rdx`, which hold no value between #! instructions: `rdx` holds an address in none of its own, `rax` what the #! memory held, as `cmpxchg` has it, and `rcx` what is written. import sakura::compiler; import binary::amd64; module sakura::native; #! `atomic`: an operation on the integer at an address, or a fence, in one step. #! @param this The selector. #! @param index The instruction. #! @param held The instruction. def selector::atomic(&mut this, index: u32, held: &inst): void { val operation := held.aux; val order := this.body.arg(index, held.argc - 1); if operation == atomic_fence { val skip := this.unless_seq_cst(order, index); if skip != never_patched { this.b.text.mfence(); this.end_unless(skip); } return; } val how := atomic_access(held.imm); val width := how.width(); val sign := how == access::i8 || how == access::i16 || how == access::i32; var direct := false; val at := this.address(this.body.arg(index, 0), index, 0, reg::rdx, reg::rcx, &mut direct); if !direct { this.null_check(at.base, width, index); } val result := held.result; if operation == atomic_load { val into := this.target(result, reg::rax); this.b.text.load(width, sign, into, at); this.put(result, into); return; } if operation == atomic_store { val value := this.word_reg(this.body.arg(index, 1), index, reg::rcx); var bits: u64 = 0; val known := this.constant_bits(order, &mut bits); if known && bits != order_seq_cst { this.b.text.store(width, at, value); return; } var plain: u64 = never_patched; if !known { this.load_into(order, index, reg::rax); this.b.text.alu_imm(alu::cmp, reg::rax, order_seq_cst as i32); plain = this.b.text.jcc(cc::not_equal); } # `xchg` writes what the memory held into the register, which may hold the # value past here. if value != reg::rcx { this.b.text.mov(reg::rcx, value); } this.b.text.xchg(width, at, reg::rcx); if !known { val done := this.b.text.jmp(); this.b.text.land(plain); this.b.text.store(width, at, value); this.b.text.land(done); } return; } if operation == atomic_exchange || operation == atomic_add || operation == atomic_sub { this.load_into(this.body.arg(index, 1), index, reg::rax); if operation == atomic_exchange { this.b.text.xchg(width, at, reg::rax); } else { if operation == atomic_sub { this.b.text.unary(unary::neg, reg::rax); } this.b.text.lock_xadd(width, at, reg::rax); } this.b.text.extend(width, sign, reg::rax, reg::rax); this.put(result, reg::rax); return; } if operation == atomic_compare_exchange { this.load_into(this.body.arg(index, 1), index, reg::rax); val desired := this.word_reg(this.body.arg(index, 2), index, reg::rcx); this.b.text.lock_cmpxchg(width, at, desired); this.b.text.extend(width, sign, reg::rax, reg::rax); this.put(result, reg::rax); return; } this.combine(index, held, at, width, sign); } #! The operations combining what the memory holds with an operand, as a loop #! working out what to write from what it held, and writing it where nothing #! wrote it since. A `cmpxchg` failing loads only the bytes it compares into #! `rax`, so what it holds is widened again before the next turn. #! @param this The selector. #! @param index The instruction. #! @param held The instruction. #! @param at The memory. #! @param width The bytes of the integer. #! @param sign Whether it is widened by its sign. def selector::combine(&mut this, index: u32, held: &inst, at: mem, width: u64, sign: bool): void { val operation := held.aux; # The operand stays where it is, or where it is in no register of its own, in # the scratch of the frame, so `rcx` is free. var operand := this.word_operand(this.body.arg(index, 1), index, reg::rcx); val extreme := operation == atomic_min || operation == atomic_max; if (operand.kind == 0 && operand.r == reg::rcx) || (operand.kind == 2 && extreme) { if operand.kind == 2 { this.b.text.mov_imm(reg::rcx, operand.imm as i64 as u64); } this.b.text.store(8, this.scratch_word(0), reg::rcx); operand = operand_of { kind = 1, m = this.scratch_word(0) }; } this.b.text.load(width, sign, reg::rax, at); val top := this.b.text.length; if extreme { if operand.kind == 0 { this.b.text.mov(reg::rcx, operand.r); } else { this.b.text.load(8, false, reg::rcx, operand.m); } this.b.text.extend(width, sign, reg::rcx, reg::rcx); this.b.text.alu(alu::cmp, reg::rax, reg::rcx); val signed := atomic_signed(held.imm); val keeps := operation == atomic_min ? (signed ? cc::less : cc::below) : (signed ? cc::greater : cc::above); this.b.text.cmov(keeps, reg::rcx, reg::rax); } else { this.b.text.mov(reg::rcx, reg::rax); val kind := operation == atomic_and ? alu::band : operation == atomic_or ? alu::bor : alu::bxor; this.apply(kind, reg::rcx, &operand); } this.b.text.lock_cmpxchg(width, at, reg::rcx); val done := this.b.text.jcc(cc::equal); this.b.text.extend(width, sign, reg::rax, reg::rax); val again := this.b.text.jmp(); this.b.text.patch(again, top); this.b.text.land(done); this.put(held.result, reg::rax); } #! What stands for no jump to patch. const never_patched: u64 = 0xFFFFFFFFFFFFFFFF; #! Begins code working only where an order is `seq_cst`: where it is known #! while compiling to be another, none; where it is known to be `seq_cst`, all of #! it, with nothing to skip; and otherwise a test of it, jumping past the code. #! @param this The selector. #! @param order The order. #! @param index The instruction. #! @returns The jump past it to patch, 0 where there is none, or #! `never_patched` where the code is not written at all. def selector::unless_seq_cst(&mut this, order: u32, index: u32): u64 { var bits: u64 = 0; if this.constant_bits(order, &mut bits) { return bits == order_seq_cst ? 0 : never_patched; } this.load_into(order, index, reg::rax); this.b.text.alu_imm(alu::cmp, reg::rax, order_seq_cst as i32); return this.b.text.jcc(cc::not_equal); } #! Ends code `unless_seq_cst` began. #! @param this The selector. #! @param skip The jump it gave. def selector::end_unless(&mut this, skip: u64): void { if skip != 0 { this.b.text.land(skip); } }