#! @file atomics.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the operations of atomics, as `core::atomic` and both compilers number #! them, and the orders they take, as `core::order` does. module sakura::compiler; # The operations an `atomic` instruction works, its `aux`. Each takes the address # first, but a fence, which takes none, and its orders last. pub const atomic_load: u32 = 0; #! (address, order): what the address holds. pub const atomic_store: u32 = 1; #! (address, value, order): writes the value, giving nothing. pub const atomic_exchange: u32 = 2; #! (address, value, order): writes the value, giving what was there. pub const atomic_compare_exchange: u32 = 3; #! (address, expected, desired, success, failure): writes `desired` where it holds `expected`, giving what was there. pub const atomic_add: u32 = 4; #! (address, operand, order): adds, wrapping, giving what was there. pub const atomic_sub: u32 = 5; #! Subtracts, wrapping. pub const atomic_and: u32 = 6; #! Keeps the bits both have. pub const atomic_or: u32 = 7; #! Keeps the bits either has. pub const atomic_xor: u32 = 8; #! Keeps the bits one has. pub const atomic_min: u32 = 9; #! Keeps the less, compared as the access is, signed or unsigned. pub const atomic_max: u32 = 10; #! Keeps the greater. pub const atomic_fence: u32 = 11; #! (order): orders the reads and writes around it, giving nothing. #! Set in an `atomic` instruction's `imm`, beside its access, where the integer #! is unsigned, which the access alone does not say of 64 bits. pub const atomic_unsigned: u64 = 0x100; #! The access an `atomic` instruction reads and writes as. #! @param imm Its `imm`. #! @returns The access. pub def atomic_access(imm: u64): access { return (imm & 0xFF) as access; } #! Whether an `atomic` instruction compares as signed integers do. #! @param imm Its `imm`. #! @returns Whether it does. pub def atomic_signed(imm: u64): bool { val how := atomic_access(imm); return (imm & atomic_unsigned) == 0 && (how == access::i8 || how == access::i16 || how == access::i32 || how == access::i64); } # The orders, as `core::order` numbers them. pub const order_relaxed: u64 = 0; pub const order_acquire: u64 = 1; pub const order_release: u64 = 2; pub const order_acq_rel: u64 = 3; pub const order_seq_cst: u64 = 4; #! The name of each atomic operation, as the printed code writes it. pub const atomic_names: strview[12] = [ "load", "store", "exchange", "compare_exchange", "add", "sub", "and", "or", "xor", "min", "max", "fence" ]; #! Whether an atomic operation gives what the address held. #! @param operation The operation. #! @returns Whether it does. pub def atomic_gives(operation: u32): bool { return operation != atomic_store && operation != atomic_fence; } #! Whether an atomic operation is one of those `core::atomic`'s integers alone #! have, which read, combine, and write. #! @param operation The operation. #! @returns Whether it is. pub def atomic_combines(operation: u32): bool { return operation >= atomic_add && operation <= atomic_max; }