Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.45.0

0.45.0 / src / back / isa.lucb

#==============================================================================================
#
#   isa - The registers an instruction set lets inline assembly name, and its instructions
#
#   DESCRIPTION:
#       base.md §8.9 lets an asm operand name a register, `x0` or `%rdi`, or write `reg`
#       for one the compiler chooses. What a name means, whether the register holds a float
#       and how wide it is, and which registers the compiler may hand out are the
#       instruction set's facts, so they are decided here, beside the generators, and the
#       lowerer asks by the target's architecture: it spells no register of its own. The
#       registers handed out are caller-saved ones the architecture's generator promises
#       not to touch while it moves an asm block's operands into place; a generator that
#       changes its scratch registers changes this table with it. Which vector operations,
#       fused multiply-adds and C library float functions a target computes in one
#       instruction is decided here too, so the lowerer and the generators agree.
#
#==============================================================================================

import back.ir.ir
import back.target as targets

# mark: Registers =============================================================================
## What a named register carries: a float or an integer, of `bits` bits.
pub struct Register:
    ## Whether the register carries a float rather than an integer.
    pub var is_float: bool
    ## The register's width in bits.
    pub var bits: u32

let arm64_ints: str[5] = ["x11", "x12", "x13", "x14", "x15"]
let arm64_floats: str[8] = ["d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23"]
let x86_64_ints: str[5] = ["%rdi", "%rsi", "%r8", "%r9", "%rcx"]
let x86_64_floats: str[8] = ["%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14", "%xmm15"]

## The integer registers `reg` operands are given, in order.
pub func scratch_ints(arch: targets.Arch) -> const str[]:
    if arch == targets.Arch.x86_64:
        return x86_64_ints
    return arm64_ints

## The float registers `reg` operands are given, in order.
pub func scratch_floats(arch: targets.Arch) -> const str[]:
    if arch == targets.Arch.x86_64:
        return x86_64_floats
    return arm64_floats

## The register `place` names on `arch`. arm64: `d` and `s` registers are floats of 64 and
## 32 bits, `w` registers 32-bit integers, every other name a 64-bit integer. x86_64: an
## `xmm` register is a 64-bit float, a 32-bit name (`eax`, `r8d`) a 32-bit integer, every
## other name a 64-bit integer. `sp`, `fp` and `lr` are the integers they are. The leading `%` of AT&T syntax is not part of the name.
pub func register_of(arch: targets.Arch, place: str) -> Register:
    let name = place.bytes[1..] if place.length > 0 and place.bytes[0] == '%' else place.bytes
    if name.length == 0:
        return Register(is_float = false, bits = 64)
    if arch == targets.Arch.x86_64:
        if name.length >= 3 and name[0] == 'x' and name[1] == 'm' and name[2] == 'm':
            return Register(is_float = true, bits = 64)
        if name[0] == 'e' or (name[0] == 'r' and name[name.length - 1] == 'd'):
            return Register(is_float = false, bits = 32)
        return Register(is_float = false, bits = 64)
    if (str)name == "sp" or (str)name == "fp" or (str)name == "lr":
        return Register(is_float = false, bits = 64)
    if name[0] == 'd':
        return Register(is_float = true, bits = 64)
    if name[0] == 's':
        return Register(is_float = true, bits = 32)
    if name[0] == 'w':
        return Register(is_float = false, bits = 32)
    return Register(is_float = false, bits = 64)

# mark: Instructions each target has ==========================================================
## Whether the architecture at instruction-set `level` (§19.5) computes vector instruction
## `op` on lanes of `shape` in one instruction sequence of its own (§5.12): the lowerer emits
## the vector form where it does and the lanes as scalars where it does not, so a generator
## implements exactly the forms claimed here for its level. A vector of one lane is a
## scalar's work on both.
pub func has_vector(arch: targets.Arch, level: u32, op: ir.Op, shape: u32) -> bool:
    let bits = ir.lane_bits(shape)
    let count = ir.lane_count(shape)
    let floats = ir.lanes_float(shape)
    if count < 2:
        return false
    if op == ir.Op.loadv or op == ir.Op.storev or op == ir.Op.vsplat or op == ir.Op.vand or op == ir.Op.vor or op == ir.Op.vxor or op == ir.Op.vnot:
        return true
    if floats:
        # NEON and SSE2 both add, subtract, multiply, divide, and negate float lanes
        return op == ir.Op.vadd or op == ir.Op.vsub or op == ir.Op.vmul or op == ir.Op.vdiv or op == ir.Op.vneg
    if arch == targets.Arch.arm64:
        # NEON: wrapping, checked (through the saturating forms), and saturating adds and
        # subtracts on every lane width; wrapping multiplies up to 32 bits; shifts on all
        if op == ir.Op.vaddw or op == ir.Op.vsubw or op == ir.Op.vaddo or op == ir.Op.vsubo or op == ir.Op.vadds or op == ir.Op.vsubs or op == ir.Op.vshl or op == ir.Op.vshr:
            return true
        return op == ir.Op.vmulw and bits <= 32
    # SSE2: wrapping adds and subtracts on every width; saturating and checked ones on bytes
    # and halfwords, checked on words through the sign; multiplies on halfwords; shifts on
    # halfwords and up, the arithmetic right shift not on doublewords
    if op == ir.Op.vaddw or op == ir.Op.vsubw:
        return true
    if op == ir.Op.vadds or op == ir.Op.vsubs:
        return bits <= 16
    if op == ir.Op.vaddo or op == ir.Op.vsubo:
        return bits <= 32
    if op == ir.Op.vmulw:
        # `pmulld` on doublewords is SSE4.1, which x86-64-v2 has
        return bits == 16 or (bits == 32 and level >= 2)
    if op == ir.Op.vshl:
        return bits >= 16
    if op == ir.Op.vshr:
        return bits >= 16 and not (bits == 64 and ir.lanes_signed(shape))
    return false

## Whether the architecture at `level` computes `a * b + c` rounded once (`ir.Op.fma`) in one
## instruction: for a scalar (`shape` 0) or for the lanes of `shape`. arm64 always has
## `fmadd` and `fmla`; x86-64 has the FMA3 forms from x86-64-v3. Elsewhere the lowerer
## calls the C library's `fma`, lane by lane for a vector.
pub func has_fma(arch: targets.Arch, level: u32, shape: u32) -> bool:
    if shape != 0 and (not ir.lanes_float(shape) or ir.vector_bytes(shape) != 16):
        return false
    return arch == targets.Arch.arm64 or level >= 3

## A float function of the C library that a backend may compute itself (§7.2): its
## instruction, and whether it is the single-precision form (`floorf`) or the double one.
pub struct FloatFunction:
    ## The instruction computing it, bit for bit as the library does.
    pub var op: ir.Op
    ## Whether it takes and returns `f32` (`floorf`); else `f64` (`floor`).
    pub var single: bool

let float_symbols: str[7] = ["sqrt", "floor", "ceil", "trunc", "round", "fabs", "fma"]
let float_ops: ir.Op[7] = [ir.Op.fsqrt, ir.Op.ffloor, ir.Op.fceil, ir.Op.ftrunc, ir.Op.fround, ir.Op.fabs, ir.Op.fma]

## The C library function an extern binds by its symbol, `sqrt`, `floor`, `ceil`, `trunc`,
## `round`, `fabs`, `fma` or the `f` form of each, as the instruction that computes it; none
## for any other symbol. This is the one place that names them: a call of such an extern
## declared with its C types is that instruction where the target has it (`has_float`) and
## the library call elsewhere; the C backend calls the compiler's builtin.
pub func float_function(symbol: str) -> FloatFunction?:
    for k in 0..<float_symbols.length:
        let name = float_symbols[k]
        if symbol == name:
            return FloatFunction(op = float_ops[k], single = false)
        if symbol.length == name.length + 1 and symbol.bytes[name.length] == 'f' and (str)symbol.bytes[0..<name.length] == name:
            return FloatFunction(op = float_ops[k], single = true)
    return none

## Whether target `t` computes the C library's float function `op` (`float_function`) in
## one instruction with the library's bits, NaNs included. arm64 has every one: `fsqrt`,
## `frintm`, `frintp`, `frintz`, `frinta`, `fabs` and `fmadd`. x86-64 has the square root
## (`sqrtss`) and the magnitude (the sign masked) at every level, fused multiply-add from
## v3 on Linux and Windows, `roundss` toward an infinity or zero from v2 (SSE4.1) on Linux,
## and no instruction rounding halves away from zero. The UCRT's `floor`, `ceil` and `trunc`
## return a signaling NaN unchanged, which `roundss` quiets, so Windows calls them.
pub func has_float(t: targets.Target, op: ir.Op) -> bool:
    if t.arch == targets.Arch.arm64:
        return true
    if t.arch != targets.Arch.x86_64:
        return false
    if op == ir.Op.fsqrt or op == ir.Op.fabs:
        return true
    if op == ir.Op.fma:
        return t.os != targets.Os.macos and has_fma(t.arch, t.level, 0)
    if op == ir.Op.ffloor or op == ir.Op.fceil or op == ir.Op.ftrunc:
        return t.os == targets.Os.linux and t.level >= 2
    return false

## Whether the C backend calls the C compiler's builtin for float function `op` on target
## `t`, which it computes with the library's bits: all of them on arm64, where clang's are
## the instructions above; only `sqrt` and `fabs` on x86-64, where GCC's inline `floor`,
## `ceil` and `trunc` return a signaling NaN unchanged and the library quiets it.
pub func has_float_builtin(t: targets.Target, op: ir.Op) -> bool:
    return t.arch == targets.Arch.arm64 or (t.arch == targets.Arch.x86_64 and (op == ir.Op.fsqrt or op == ir.Op.fabs))

# mark: Inline assembly =======================================================================
## Whether `arch` is the architecture an `asm ARCH` block names (§8.9).
pub func is_named(arch: targets.Arch, name: str) -> bool:
    return name == targets.arch_name(arch)

# mark: Tests =================================================================================
test "the registers of each architecture":
    assert(register_of(targets.Arch.arm64, "d3").is_float)
    assert(register_of(targets.Arch.arm64, "s3").bits == 32)
    assert(register_of(targets.Arch.arm64, "w7").bits == 32)
    assert(register_of(targets.Arch.arm64, "x7").bits == 64)
    assert(register_of(targets.Arch.arm64, "sp").bits == 64)
    assert(register_of(targets.Arch.x86_64, "%xmm3").is_float)
    assert(register_of(targets.Arch.x86_64, "%eax").bits == 32)
    assert(register_of(targets.Arch.x86_64, "%r8d").bits == 32)
    assert(register_of(targets.Arch.x86_64, "rax").bits == 64)
    assert(not register_of(targets.Arch.x86_64, "%rsi").is_float)
    assert(scratch_ints(targets.Arch.arm64).length == 5)
    assert(scratch_floats(targets.Arch.x86_64)[0] == "%xmm8")

test "fused multiply-add where each target has it":
    let quad = ir.lanes(32, 4, true, true)
    let pair = ir.lanes(32, 2, true, true)
    assert(has_fma(targets.Arch.arm64, 1, 0))
    assert(has_fma(targets.Arch.arm64, 1, quad))
    assert(not has_fma(targets.Arch.arm64, 1, pair))
    assert(not has_fma(targets.Arch.x86_64, 2, 0))
    assert(has_fma(targets.Arch.x86_64, 3, quad))
    assert(not has_fma(targets.Arch.x86_64, 3, ir.lanes(32, 4, false, true)))

func target_at(name: str, level: u32) -> targets.Target:
    var t = targets.Target.named(name) else trap("no such target")
    t.level = level
    return t

test "the C library's float functions and where each is one instruction":
    if let floor = float_function("floorf"):
        assert(floor.op == ir.Op.ffloor and floor.single)
    else:
        assert(false)
    if let fma = float_function("fma"):
        assert(fma.op == ir.Op.fma and not fma.single)
    else:
        assert(false)
    assert(float_function("floorl") == none and float_function("sin") == none and float_function("f") == none)
    let mac = target_at("arm64-macos", 1)
    let linux = target_at("x86_64-linux", 1)
    let linux_v2 = target_at("x86_64-linux", 2)
    assert(has_float(target_at("x86_64-linux", 3), ir.Op.fma))
    let windows_v3 = target_at("x86_64-windows", 3)
    assert(has_float(mac, ir.Op.fround) and has_float(mac, ir.Op.fma))
    assert(has_float(linux, ir.Op.fsqrt) and not has_float(linux, ir.Op.ffloor) and not has_float(linux, ir.Op.fma))
    assert(has_float(linux_v2, ir.Op.ftrunc) and not has_float(linux_v2, ir.Op.fround) and not has_float(linux_v2, ir.Op.fma))
    assert(has_float(windows_v3, ir.Op.fabs) and not has_float(windows_v3, ir.Op.ffloor) and has_float(windows_v3, ir.Op.fma))
    assert(has_float_builtin(mac, ir.Op.ffloor) and has_float_builtin(linux, ir.Op.fsqrt) and not has_float_builtin(linux, ir.Op.ffloor))