Affix

 view release on metacpan or  search on metacpan

infix/src/arch/x64/abi_x64_emitters.c  view on Meta::CPAN

/**
 * Copyright (c) 2025 Sanko Robinson
 *
 * This source code is dual-licensed under the Artistic License 2.0 or the MIT License.
 * You may choose to use this code under the terms of either license.
 *
 * SPDX-License-Identifier: (Artistic-2.0 OR MIT)
 *
 * The documentation blocks within this file are licensed under the
 * Creative Commons Attribution 4.0 International License (CC BY 4.0).
 *
 * SPDX-License-Identifier: CC-BY-4.0
 */
/**
 * @file abi_x64_emitters.c
 * @brief Implements internal helper functions for emitting x86-64 machine code.
 * @ingroup internal_abi_x64
 *
 * @internal
 * This file provides the concrete implementations for the low-level x86-64
 * instruction emitters. These functions are the fundamental building blocks
 * used by both the Windows x64 and System V ABI implementations.
 *
 * By centralizing these functions, we encapsulate the complex details of x86-64
 * instruction encoding (opcodes, ModR/M bytes, REX prefixes). For a definitive
 * reference, see the Intel 64 and IA-32 Architectures Software Developer's Manuals.
 * @endinternal
 */
#include "arch/x64/abi_x64_emitters.h"
#include "common/utility.h"
#include <assert.h>
#include <string.h>
// Helper defines for REX prefix bits, making the code more readable.
#define REX_W (1 << 3)  // 64-bit operand size
#define REX_R (1 << 2)  // Extends ModR/M 'reg' field
#define REX_X (1 << 1)  // Extends SIB 'index' field
#define REX_B (1 << 0)  // Extends ModR/M 'r/m' or SIB 'base' field

/**
 * @internal
 * @brief Emits `endbr64` (Terminate Indirect Branch 64-bit) for Intel IBT / CET.
 * @details Opcode: F3 0F 1E FA
 */
INFIX_INTERNAL void emit_endbr64(code_buffer * buf) {
    EMIT_BYTES(buf, 0xF3, 0x0F, 0x1E, 0xFA);
}

// Instruction Encoding Helpers
/*
 * @internal
 * @brief Emits an x86-64 REX prefix byte.
 * @details The REX prefix is a single byte (0x40-0x4F) used in 64-bit mode to:
 * - Set operand size to 64 bits (W bit).
 * - Extend the register fields to access R8-R15 (R, X, B bits).
 */
INFIX_INTERNAL void emit_rex_prefix(code_buffer * buf, bool w, bool r, bool x, bool b) {
    uint8_t rex_byte = 0x40;
    if (w)
        rex_byte |= REX_W;
    if (r)
        rex_byte |= REX_R;
    if (x)
        rex_byte |= REX_X;
    if (b)
        rex_byte |= REX_B;
    emit_byte(buf, rex_byte);
}
/*
 * @internal
 * @brief Emits an x86-64 ModR/M byte.
 * @details The ModR/M byte is a crucial part of many instructions, specifying the addressing mode.
 * It encodes register operands and memory operands.
 */
INFIX_INTERNAL void emit_modrm(code_buffer * buf, uint8_t mod, uint8_t reg_opcode, uint8_t rm) {
    uint8_t modrm_byte = (mod << 6) | (reg_opcode << 3) | rm;
    emit_byte(buf, modrm_byte);
}
// GPR <-> Immediate Value Emitters
/**
 * @internal
 * @brief Emits `mov r64, imm64` to load a 64-bit immediate value into a register.
 * @details Instruction Breakdown: MOV r64, imm64
 * Opcode format: REX.W + B8+rd imm64
 */
INFIX_INTERNAL void emit_mov_reg_imm64(code_buffer * buf, x64_gpr reg, uint64_t imm) {
    emit_rex_prefix(buf, 1, 0, 0, reg >= R8_REG);
    emit_byte(buf, 0xB8 + (reg % 8));
    emit_int64(buf, imm);
}
/**
 * @internal
 * @brief Emits `mov r/m64, imm32` (sign-extended) to load a 32-bit immediate into a register.
 * @details Instruction Breakdown: MOV r/m64, imm32 (sign-extended)
 * Opcode format: REX.W + C7 /0 id
 */
INFIX_INTERNAL void emit_mov_reg_imm32(code_buffer * buf, x64_gpr reg, int32_t imm) {
    emit_rex_prefix(buf, 1, 0, 0, reg >= R8_REG);
    emit_byte(buf, 0xC7);
    emit_modrm(buf, 3, 0, reg % 8);  // mod=11 (register), reg=/0
    emit_int32(buf, imm);
}



( run in 1.426 second using v1.01-cache-2.11-cpan-1bcd28dbaf1 )