Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.44.0

0.44.0 / src / back / native / x86_64 / registers.lucb

#==============================================================================================
#
#   x86_64/registers - Registers and immediates
#
#   DESCRIPTION:
#       The register each class and number names, immediates that fit, and a slot's
#       or a temporary's place in the frame.
#
#==============================================================================================
extend Generator:
    # mark: Registers, immediates, and the frame ===============================================

    ## `value` into an integer register: `movl` for a 32-bit name, `movq` when the value
    ## fits a sign-extended immediate, `movabsq` otherwise.
    func load_immediate(reg: str, value: i64) -> !:
        if is_narrow(reg):
            try self.writer.line("movl $")
            try self.writer.put_u64((u64)value & 4294967295)
            try self.writer.put(", ")
            try self.writer.put(reg)
            return
        if value >= -2147483648 and value <= 2147483647:
            try self.writer.line("movq $")
        else:
            try self.writer.line("movabsq $")
        try self.writer.put_i64(value)
        try self.writer.put(", ")
        try self.writer.put(reg)

    ## `-offset(%rbp)`: the home `offset` bytes below the frame pointer. On Windows %rbp is
    ## the fixed frame's bottom, and a slot of the dynamic area lies below it.
    func frame_operand(offset: u64) -> !:
        if self.convention.windows:
            if offset > self.fr.size:
                try self.writer.put("-")
                try self.writer.put_u64(offset - self.fr.size)
            else:
                try self.writer.put_u64(self.fr.size - offset)
        else:
            try self.writer.put("-")
            try self.writer.put_u64(offset)
        try self.writer.put("(%rbp)")

    func frame_store(reg: str, cls: ir.Class, offset: u64) -> !:
        try self.writer.line3(move_of(cls), reg, ", ")
        try self.frame_operand(offset)

    func frame_load(reg: str, cls: ir.Class, offset: u64) -> !:
        try self.writer.line(move_of(cls))
        try self.frame_operand(offset)
        try self.writer.put(", ")
        try self.writer.put(reg)

    ## Where a load or store by `op` through `address` reaches its bytes: a slot, or a
    ## frame address formed at its use (`remat`), addressed off the frame pointer
    ## directly, `below` it; an address a temporary holds in an integer register, through
    ## that register; any other address (or a half's access) into %r10 first. The base
    ## register comes back with it, "" for the frame.
    func memory_place(address: ir.Ref, op: ir.Op) -> (u64?, str)!:
        if op != ir.Op.loadhf and op != ir.Op.storehf:
            if address.kind == ir.RefKind.slot:
                return (self.slot_home(address.index), "")
            if address.kind == ir.RefKind.temp:
                let code = self.fr.temp_regs.get((usize)address.index - 1)
                if code == 0:
                    if let below = self.fr.rematerialised(address.index):
                        return (below, "")
                elif not self.fr.float_code(code):
                    return (none, self.q_name(code))
        try self.address_operand("%r10", address)
        return (none, "%r10")

    ## The parameters `receive_params` left in their argument registers, moved into their
    ## entry loads' results as parallel copies (`incoming`), one of the integer registers and
    ## one of the float ones, since a result may live in another argument's register;
    ## answers which instructions that leaves with nothing to do. %r11 parks an integer
    ## argument to break a cycle, the first float scratch register a float one; %rax carries
    ## a value to a frame home, all written before any is parked.
    func receive_directly(moves: list.List[incoming.Move]*, uses: const u32[]) -> bool[]!:
        var received = try new bool[self.f.instrs.count + 1] ---
        for k in 0..<received.length:
            received[k] = false
        # the integer argument registers: rcx, rdx, r8 and r9 on Windows
        let names = windows_arguments[0..<4] if self.convention.windows else argument_names[0..<6]
        var sources = try new u8[moves.count + 1] ---
        defer free(sources)
        var destinations = try new u8[moves.count + 1] ---
        defer free(destinations)
        var float_sources = try new u8[moves.count + 1] ---
        defer free(float_sources)
        var float_destinations = try new u8[moves.count + 1] ---
        defer free(float_destinations)
        var float_moves = try new usize[moves.count + 1] ---
        defer free(float_moves)
        var floats: usize = 0
        for k in 0..<moves.count:
            let m = moves.get(k)
            received[(usize)m.at] = true
            sources[k] = incoming.no_argument
            destinations[k] = incoming.no_argument
            let n = self.f.instrs.get((usize)m.at)
            if (usize)n.dst < uses.length and uses[(usize)n.dst] == 0:
                # an unread parameter: its register may be another's, which a copy of this
                # one into it would overwrite
                continue
            let code = self.fr.temp_regs.get((usize)n.dst - 1)
            if m.float:
                if code != 0:
                    # the argument's xmm register is its number
                    float_sources[floats] = m.argument
                    float_destinations[floats] = xmm_number(self.class_name(code, n.cls))
                    float_moves[floats] = k
                    floats += 1
                else:
                    try self.frame_store(float_argument_names[(usize)m.argument], n.cls, self.temp_home(n.dst))
                continue
            sources[k] = m.argument
            if code != 0:
                let home = self.class_name(code, ir.Class.l)
                for a in 0..<names.length:
                    if names[a] == home:
                        destinations[k] = u8(a)
        var steps = try incoming.sequence(sources[..<moves.count], destinations[..<moves.count])
        defer steps.destroy()
        for step in steps.span():
            let m = moves.get((usize)step.index)
            if m.float or sources[(usize)step.index] == incoming.no_argument:
                continue
            if step.park:
                try self.writer.line3("movq ", names[(usize)m.argument], ", %r11")
                continue
            try self.receive_one(self.f.instrs.get((usize)m.at), "%r11" if step.parked else names[(usize)m.argument])
        var float_steps = try incoming.sequence(float_sources[..<floats], float_destinations[..<floats])
        defer float_steps.destroy()
        let park = self.reg(ir.Class.v, 0)
        for step in float_steps.span():
            let k = (usize)step.index
            let arrived = float_argument_names[(usize)float_sources[k]]
            if step.park:
                try self.writer.line4("movaps ", arrived, ", ", park)
                continue
            let n = self.f.instrs.get((usize)moves.get(float_moves[k]).at)
            let home = xmm_names[(usize)float_destinations[k]]
            let read = park if step.parked else arrived
            if read != home:
                try self.register_move(home, read, n.cls, 0)
        return received

    ## Entry load `n`'s result from the integer register `wide`: its argument's, or %r11;
    ## extended as the load from the parameter's slot would have.
    func receive_one(n: ir.Instr, wide: str) -> !:
        let code = self.fr.temp_regs.get((usize)n.dst - 1)
        let d = self.class_name(code, n.cls) if code != 0 else ("%rax" if n.cls == ir.Class.l else "%eax")
        let long = n.cls == ir.Class.l
        match n.op:
            .loadl:
                if d != wide:
                    try self.writer.line4("movq ", wide, ", ", d)
            .loaduw:
                try self.writer.line4("movl ", narrow_name(wide), ", ", d)
            .loadsw:
                try self.writer.line4("movslq " if long else "movl ", narrow_name(wide), ", ", d)
            .loaduh:
                try self.writer.line4("movzwl ", word_name(wide), ", ", narrow_name(d))
            .loadsh:
                try self.writer.line4("movswq " if long else "movswl ", word_name(wide), ", ", d)
            .loadub:
                try self.writer.line4("movzbl ", byte_name(wide), ", ", narrow_name(d))
            _:
                try self.writer.line4("movsbq " if long else "movsbl ", byte_name(wide), ", ", d)
        if code == 0:
            try self.frame_store(d, n.cls, self.temp_home(n.dst))

    ## A load or store at `offset(base)`, the add that formed the address folded into it
    ## (`selection.offset_access`).
    func offset_access(n: ir.Instr, base: ir.Ref, offset: i64) -> !:
        if (u8)n.op <= (u8)ir.Op.loadd:
            let lc = ir.load_class(n.op)
            let d = self.target(n.dst, lc, self.reg(lc, 0))
            let b = try self.source(base, ir.Class.l, "%r10")
            try self.writer.line(load_mnemonic(n.op))
            try self.writer.put_i64(offset)
            try self.writer.put("(")
            try self.writer.put(b)
            try self.put2("), ", d)
            try self.finish(n.dst, lc, d)
            return
        let cls = ir.store_class(n.op)
        let v = try self.source(n.a, cls, self.reg(cls, 0))
        let b = try self.source(base, ir.Class.l, "%r10")
        if n.op == ir.Op.storeb:
            try self.writer.line3("movb ", byte_name(v), ", ")
        elif n.op == ir.Op.storeh:
            try self.writer.line3("movw ", word_name(v), ", ")
        else:
            try self.writer.line3(store_mnemonic(n.op), v, ", ")
        try self.writer.put_i64(offset)
        try self.writer.put("(")
        try self.writer.put(b)
        try self.writer.put(")")

    ## A load or store through `base + (index << shift)` (`selection.scaled_access` and
    ## `summed_access`), the scaling and the sum done by the addressing: `movl
    ## (%rbx,%rcx,4), %eax`.
    func indexed_access(n: ir.Instr, base: ir.Ref, index: ir.Ref, shift: u32) -> !:
        if (u8)n.op <= (u8)ir.Op.loadd:
            let lc = ir.load_class(n.op)
            let d = self.target(n.dst, lc, self.reg(lc, 0))
            let b = try self.source(base, ir.Class.l, "%r10")
            let x = try self.source(index, ir.Class.l, "%r11")
            try self.writer.line(load_mnemonic(n.op))
            try self.put_indexed(b, x, shift)
            try self.put2(", ", d)
            try self.finish(n.dst, lc, d)
            return
        let cls = ir.store_class(n.op)
        let v = try self.source(n.a, cls, self.reg(cls, 0))
        let b = try self.source(base, ir.Class.l, "%r10")
        let x = try self.source(index, ir.Class.l, "%r11")
        if n.op == ir.Op.storeb:
            try self.writer.line3("movb ", byte_name(v), ", ")
        elif n.op == ir.Op.storeh:
            try self.writer.line3("movw ", word_name(v), ", ")
        else:
            try self.writer.line3(store_mnemonic(n.op), v, ", ")
        try self.put_indexed(b, x, shift)

    ## The memory operand `(base,index,2^shift)`.
    func put_indexed(base: str, index: str, shift: u32) -> !:
        try self.writer.put("(")
        try self.put2(base, ",")
        try self.put2(index, ",")
        try self.writer.put_u64((u64)1 << (u64)shift)
        try self.writer.put(")")

    ## The memory operand `memory_place` chose: off the frame pointer, or through `base`.
    func put_place(place: (u64?, str)) -> !:
        let (frame_place, base) = place
        if let below = frame_place:
            try self.frame_operand(below)
        else:
            try self.writer.put("(")
            try self.put2(base, ")")

    func put2(a: str, b: str) -> !:
        try self.writer.put(a)
        try self.writer.put(b)

    ## `reg = rbp - offset`.
    func frame_address(reg: str, offset: u64) -> !:
        try self.writer.line("leaq ")
        try self.frame_operand(offset)
        try self.writer.put(", ")
        try self.writer.put(reg)

    ## Before the barrier at instruction `at`: each temporary that keeps a caller-saved
    ## register across it goes to its home (`frame.Frame.crossings`).
    func save_crossing(at: usize) -> !:
        while self.crossing < self.fr.crossings.count and (usize)self.fr.crossings.get(self.crossing).at < at:
            self.crossing += 1
        var k = self.crossing
        while k < self.fr.crossings.count and (usize)self.fr.crossings.get(k).at == at:
            let c = self.fr.crossings.get(k)
            let code = self.fr.temp_regs.get((usize)c.temp - 1)
            let cls = self.f.temp_class(c.temp)
            try self.frame_store(self.class_name(code, cls), cls, self.temp_home(c.temp))
            k += 1

    ## After it: each comes back from its home.
    func restore_crossing(at: usize) -> !:
        while self.crossing < self.fr.crossings.count and (usize)self.fr.crossings.get(self.crossing).at == at:
            let c = self.fr.crossings.get(self.crossing)
            let code = self.fr.temp_regs.get((usize)c.temp - 1)
            let cls = self.f.temp_class(c.temp)
            try self.frame_load(self.class_name(code, cls), cls, self.temp_home(c.temp))
            self.crossing += 1

    func temp_home(t: u32) -> u64:
        return self.fr.temp_home(t)

    func slot_home(s: u32) -> u64:
        return self.fr.slot_home(s)

    ## The scratch register of class `cls` numbered `n` (0, 1, 2): rax, r11, r10 for the
    ## integers, xmm14, xmm15 and xmm13 for the floats and vectors. r10 and r11 also hold the
    ## addresses of loads and stores, which use no other scratch but rax. On Windows, where
    ## xmm6–xmm15 are nonvolatile and a function using one saves it, the first two float
    ## scratch registers are the volatile xmm4 and xmm5, which carry no argument there, and
    ## the third is xmm15. No temporary lives in any of them, the third float one only in a
    ## function that needs it (`third_scratch`).
    func reg(cls: ir.Class, n: u32) -> str:
        if cls == ir.Class.w:
            return "%eax" if n == 0 else ("%r11d" if n == 1 else "%r10d")
        if cls == ir.Class.l:
            return "%rax" if n == 0 else ("%r11" if n == 1 else "%r10")
        if self.convention.windows:
            return "%xmm4" if n == 0 else ("%xmm5" if n == 1 else "%xmm15")
        return "%xmm14" if n == 0 else ("%xmm15" if n == 1 else "%xmm13")

    ## Register code `code` viewed as class `cls`, under the target's convention.
    func class_name(code: u8, cls: ir.Class) -> str:
        if cls == ir.Class.w:
            return self.l_name(code)
        return self.q_name(code)

    ## Register code `code`'s 64-bit name (an xmm register's only one).
    func q_name(code: u8) -> str:
        return windows_q[(usize)code] if self.convention.windows else sysv_q[(usize)code]

    ## Register code `code`'s 32-bit name.
    func l_name(code: u8) -> str:
        return windows_l[(usize)code] if self.convention.windows else sysv_l[(usize)code]

    ## Register code `code`'s 16-bit name.
    func w_name(code: u8) -> str:
        return windows_w[(usize)code] if self.convention.windows else sysv_w[(usize)code]

    ## Register code `code`'s 8-bit name.
    func b_name(code: u8) -> str:
        return windows_b[(usize)code] if self.convention.windows else sysv_b[(usize)code]

    ## Where temporary `r` lives in the frame, below the frame pointer, when it lives there
    ## and is no frame address formed at its use; none otherwise.
    func frame_home(r: ir.Ref) -> u64?:
        if r.kind != ir.RefKind.temp or self.fr.temp_regs.get((usize)r.index - 1) != 0 or self.fr.is_rematerialised(r.index):
            return none
        return self.temp_home(r.index)

    ## The register holding `r` as class `cls` when `r` is a temporary living in one and the
    ## class is an integer one, else `scratch` after loading it there.
    func source(r: ir.Ref, cls: ir.Class, scratch: str) -> str!:
        if r.kind == ir.RefKind.temp:
            let code = self.fr.temp_regs.get((usize)r.index - 1)
            if code != 0 and self.fr.float_code(code) == not is_int_class(cls):
                return self.class_name(code, cls)
        try self.operand(scratch, cls, r)
        return scratch

    ## The register to compute temporary `dst` into: its own when it lives in one and the
    ## class is an integer one, else `scratch`, which `finish` then stores.
    func target(dst: u32, cls: ir.Class, scratch: str) -> str:
        if dst == 0:
            return scratch
        let code = self.fr.temp_regs.get((usize)dst - 1)
        if code != 0 and self.fr.float_code(code) == not is_int_class(cls):
            return self.class_name(code, cls)
        return scratch

    ## After a result landed in `reg`: nothing when that is the temporary's own register.
    func finish(dst: u32, cls: ir.Class, reg: str) -> !:
        if dst == 0:
            return
        let code = self.fr.temp_regs.get((usize)dst - 1)
        if code != 0 and self.class_name(code, cls) == reg:
            return
        try self.store_temp(reg, cls, dst)

    ## Load temporary `t` into `reg` as class `cls`: a move when it lives in a register,
    ## else from its home. An integer register read as a float moves its bits over.
    func load_temp(reg: str, cls: ir.Class, t: u32) -> !:
        let code = self.fr.temp_regs.get((usize)t - 1)
        if code != 0:
            let home = self.class_name(code, cls)
            if home != reg:
                try self.register_move(reg, home, cls, code)
            return
        if let bits = self.fr.constant_of(t):
            try self.constant(reg, cls, bits)
            return
        if let below = self.fr.rematerialised(t):
            # a frame address, formed here (`remat`)
            if is_int_class(cls):
                try self.frame_address(wide_name(reg), below)
            else:
                try self.frame_address("%r11", below)
                try self.writer.line3("movq %r11, ", reg, "")
            return
        try self.frame_load(reg, cls, self.temp_home(t))

    func store_temp(reg: str, cls: ir.Class, t: u32) -> !:
        let code = self.fr.temp_regs.get((usize)t - 1)
        if code != 0:
            let home = self.class_name(code, cls)
            if home != reg:
                try self.register_move(home, reg, cls, code)
            return
        try self.frame_store(reg, cls, self.temp_home(t))

    ## `to = from` as class `cls` between registers, one of which is the home of a temporary
    ## in register `code`: integers move as words, floats and vectors with the SSE moves,
    ## and a float read as an integer word or the reverse moves its bits across.
    func register_move(target_reg: str, from_reg: str, cls: ir.Class, code: u8) -> !:
        let to_xmm = target_reg.bytes[1] == 'x'
        let from_xmm = from_reg.bytes[1] == 'x'
        if to_xmm and from_xmm:
            try self.writer.line4("movdqa " if cls == ir.Class.v else ("movaps " if cls == ir.Class.s else "movapd "), from_reg, ", ", target_reg)
        elif not to_xmm and not from_xmm:
            try self.writer.line4("movl " if cls == ir.Class.w else "movq ", from_reg, ", ", target_reg)
        else:
            try self.writer.line4("movd " if cls == ir.Class.w or cls == ir.Class.s or cls == ir.Class.h else "movq ", from_reg, ", ", target_reg)
        _ = code

    ## An operand into a register of class `cls`.
    func operand(reg: str, cls: ir.Class, r: ir.Ref) -> !:
        if r.kind == ir.RefKind.temp:
            try self.load_temp(reg, cls, r.index)
            return
        if r.kind == ir.RefKind.constant:
            if ir.is_float_class(cls):
                if cls == ir.Class.d:
                    try self.load_immediate("%rax", r.value)
                    try self.writer.line3("movq %rax, ", reg, "")
                else:
                    try self.load_immediate("%eax", r.value)
                    try self.writer.line3("movd %eax, ", reg, "")
                return
            try self.load_immediate(reg, r.value)
            return
        try self.address_operand(wide_name(reg), r)

    ## The address a slot, global, function, or text refers to, into a 64-bit register.
    func address_operand(reg: str, r: ir.Ref) -> !:
        if r.kind == ir.RefKind.slot:
            try self.frame_address(reg, self.slot_home(r.index))
        elif r.kind == ir.RefKind.global and r.value == 0:
            try self.writer.line("leaq ")
            try self.symbol(r.name)
            try self.writer.put("(%rip), ")
            try self.writer.put(reg)
        elif r.kind == ir.RefKind.global or r.kind == ir.RefKind.function:
            try object.external_address(&self.object_format, &self.writer.out, r.name, reg)
        elif r.kind == ir.RefKind.text:
            try self.writer.line("leaq ")
            try self.text_label(r.index)
            try self.writer.put("(%rip), ")
            try self.writer.put(reg)
        elif r.kind == ir.RefKind.temp:
            try self.load_temp(reg, ir.Class.l, r.index)
        elif r.kind == ir.RefKind.constant:
            try self.load_immediate(reg, r.value)
        else:
            try self.writer.line3("movq $0, ", reg, "")

    # mark: Float constants ===================================================================

    ## The float constant of `bits` formed in `reg` as class `cls` (`frame.Frame.constant_of`):
    ## a zero by clearing the register, any other read from the function's literal pool; an
    ## integer register (the constant's bits read as an integer) takes them as an immediate.
    ## No form touches the flags, so a constant may be formed between a comparison and the
    ## branch or `cmov` on it.
    func constant(reg: str, cls: ir.Class, bits: u64) -> !:
        if reg.bytes[1] != 'x':
            try self.load_immediate(reg, (i64)bits)
            return
        if bits == 0:
            try self.writer.line4("xorps ", reg, ", ", reg)
            return
        try self.writer.line("movsd " if cls == ir.Class.d else "movss ")
        try self.pooled(bits)
        try self.put2(", ", reg)

    ## `label(%rip)`: the literal pool's entry for `bits`, added when it has none.
    func pooled(bits: u64) -> !:
        var k: usize = 0
        while k < self.pool.count and self.pool.get(k) != bits:
            k += 1
        if k == self.pool.count:
            try self.pool.push(bits)
        try self.pool_label(u32(k))
        try self.writer.put("(%rip)")

    ## Whether `r` is a float constant read from the literal pool where it is used, which an
    ## arithmetic instruction may then read itself; its bits.
    func pooled_constant(r: ir.Ref) -> u64?:
        if r.kind != ir.RefKind.temp:
            return none
        if let bits = self.fr.constant_of(r.index):
            if bits != 0:
                return bits
        return none

    ## After the function's end, which leaves the text section current: its literal pool,
    ## eight bytes an entry (a single's in the low four), in read-only data, and the text
    ## section again.
    func finish_pool() -> !:
        if self.pool.count == 0:
            return
        try self.object_format.read_only(&self.writer.out)
        try self.writer.put("    .p2align 3")
        for k in 0..<self.pool.count:
            try self.writer.put("\n")
            try self.pool_label(u32(k))
            try self.writer.put(":\n    .quad ")
            try self.writer.put_u64(self.pool.get(k))
        try self.writer.put("\n")
        try self.object_format.text_section(&self.writer.out)
        self.pool.count = 0

## The move that carries a register of class `cls` to or from memory.
func move_of(cls: ir.Class) -> str:
    if cls == ir.Class.v:
        return "movdqu "
    if cls == ir.Class.w:
        return "movl "
    if cls == ir.Class.l:
        return "movq "
    if cls == ir.Class.d:
        return "movsd "
    return "movss "