Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions src/backend/x86/bal_x86_tier1_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1045,10 +1045,12 @@ translate_add_sub_reg(bal_tier1_compiler_t *BAL_RESTRICT com
const uint8_t rm = (uint8_t)extract_operand_value(instruction, &operand_cursor[3]);
const uint32_t shift_type = extract_operand_value(instruction, &operand_cursor[4]);

if (BAL_UNLIKELY(shift_amount != 0))
const bool is_32bit = BAL_OPERAND_TYPE_REGISTER_32 == operand_cursor[0].type;

if (BAL_UNLIKELY(is_32bit && 0 != shift_amount))
{
BAL_LOG_ERROR(&bal_thread_logger,
"Aborting function: Tier 1 does not support shift amounts != 0 yet: %s",
"Aborting function: Tier 1 does not support 32-bit shifted operands: %s",
metadata->name);
compiler->status = BAL_ERROR_UNKNOWN_INSTRUCTION;
return;
Expand All @@ -1072,15 +1074,51 @@ translate_add_sub_reg(bal_tier1_compiler_t *BAL_RESTRICT com
bal_x86_register_t x86_rm = allocate_x86_register(compiler, rm, skip_load_rm);
bal_x86_register_t x86_rd;

if (true == is_discarded_result)
if (0 != shift_amount)
{
x86_rd = BAL_X86_REGISTER_DISCARD_RESULT;
const bal_x86_macro_t mov_macro = {
const bal_x86_macro_t copy_macro = {
.opcode = BAL_X86_MACRO_MOV_REGISTER_REGISTER,
.destination = x86_rd,
.source = x86_rn,
.destination = BAL_X86_REGISTER_DISCARD_RESULT,
.source = x86_rm,
};
bal_sliding_window_push(&compiler->window, mov_macro);
bal_sliding_window_push(&compiler->window, copy_macro);

bal_x86_macro_opcode_t shift_opcode = BAL_X86_MACRO_SHL_REGISTER_IMMEDIATE;

if (0b01 == shift_type)
{
shift_opcode = BAL_X86_MACRO_SHR_REGISTER_IMMEDIATE;
}
else if (0b10 == shift_type)
{
shift_opcode = BAL_X86_MACRO_SAR_REGISTER_IMMEDIATE;
}

const bal_x86_macro_t shift_macro = {
.opcode = shift_opcode,
.destination = BAL_X86_REGISTER_DISCARD_RESULT,
.immediate_or_offset = shift_amount,
};
bal_sliding_window_push(&compiler->window, shift_macro);
x86_rm = BAL_X86_REGISTER_DISCARD_RESULT;
}

if (true == is_discarded_result)
{
if (0 != shift_amount)
{
x86_rd = x86_rn;
}
else
{
x86_rd = BAL_X86_REGISTER_DISCARD_RESULT;
const bal_x86_macro_t mov_macro = {
.opcode = BAL_X86_MACRO_MOV_REGISTER_REGISTER,
.destination = x86_rd,
.source = x86_rn,
};
bal_sliding_window_push(&compiler->window, mov_macro);
}
}
else
{
Expand Down Expand Up @@ -1131,7 +1169,6 @@ translate_add_sub_reg(bal_tier1_compiler_t *BAL_RESTRICT com
}
}

const bool is_32bit = BAL_OPERAND_TYPE_REGISTER_32 == operand_cursor[0].type;
bal_x86_macro_opcode_t arithmetic_opcode;

if (true == is_sub)
Expand All @@ -1152,6 +1189,12 @@ translate_add_sub_reg(bal_tier1_compiler_t *BAL_RESTRICT com
};
bal_sliding_window_push(&compiler->window, arithmetic_macro);

if (true == is_discarded_result && 0 != shift_amount)
{
compiler->arm_to_x86[rn] = -1;
compiler->x86_to_arm[x86_rn] = -1;
}

if (true == is_setting_flags)
{
const bal_x86_macro_t set_n_macro = {
Expand Down
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ target_link_libraries(test_bal_assembler_sub PUBLIC BallisticTestAssemblerFixtur

add_ballistic_test(test_bal_decoder_integrity unit/bal_decoder/test_bal_decoder_integrity.c)

add_ballistic_test(test_bal_tier1_add_sub unit/bal_tier1/test_bal_tier1_add_sub.c)

add_ballistic_test(test_bal_jit_debug_lifecycle unit/bal_jit_debug/test_bal_jit_debug_lifecycle.c)
add_ballistic_test(test_bal_jit_debug_add_block unit/bal_jit_debug/test_bal_jit_debug_add_block.c)

Expand Down Expand Up @@ -93,7 +95,6 @@ set_tests_properties(doctest PROPERTIES
ENVIRONMENT "LD_LIBRARY_PATH=${LUA_LD_PATH};DYLD_LIBRARY_PATH=${LUA_DYLD_LIBRARY_PATH};DOCTEST_CC=${DOCTEST_CC};DOCTEST_CFLAGS=${DOCTEST_CFLAGS};DOCTEST_LTO=${DOCTEST_LTO};DOCTEST_LIB_DIR=${BALLISTIC_LIBRARIES_DIRECTORY}"
)


# TODO (GloriousTaco): Support IPC on Windows.
if (UNIX)
if (LINUX)
Expand Down
261 changes: 261 additions & 0 deletions tests/unit/bal_tier1/test_bal_tier1_add_sub.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
#include "bal_engine.h"
#include "bal_engine_flags.h"
#include "bal_memory.h"
#include "unity.h"
#include <string.h>

#define ARM64_RET_ENCODING 0xD65F03C0U
#define TEST_GUEST_MEMORY_SIZE 4096U

#define ADD_X8_X9_X10_LSL4 0x8B0A1128U
#define ADD_X8_X9_X10_LSR4 0x8B4A1128U
#define ADD_X8_X9_X10_ASR4 0x8B8A1128U
#define ADD_X8_X9_X10_LSL63 0x8B0AFD28U
#define ADD_X8_X9_X8_LSL4 0x8B081128U
#define ADD_X8_X8_X10_LSL4 0x8B0A1108U
#define ADD_X8_X9_X10 0x8B0A0128U
#define SUB_X8_X9_X10_LSL4 0xCB0A1128U
#define SUB_X8_X9_X8_LSL4 0xCB081128U
#define CMP_X8_X9_LSL4 0xEB0A113FU
#define CMP_X8_X9 0xEB0A013FU
#define CMN_X8_X9_ASR4 0xAB8A113FU

static uint32_t bal_test_guest_memory[TEST_GUEST_MEMORY_SIZE / sizeof(uint32_t)];

void
setUp(void)
{
(void)memset(bal_test_guest_memory, 0, sizeof(bal_test_guest_memory));
}

void
tearDown(void)
{
}

static void
run_single_guest_instruction(const uint32_t instruction, bal_cpu_t *BAL_RESTRICT cpu)
{
bal_test_guest_memory[0] = instruction;
bal_test_guest_memory[1] = ARM64_RET_ENCODING;

bal_allocator_t allocator = {};
bal_allocator_default_init(&allocator);

bal_memory_interface_t memory_interface = {};
TEST_ASSERT_EQUAL(BAL_SUCCESS,
bal_flat_translation_interface_init(&allocator,
&memory_interface,
bal_test_guest_memory,
sizeof(bal_test_guest_memory)));

cpu->pc = 0ULL;

bal_engine_t engine = {};
TEST_ASSERT_EQUAL(BAL_SUCCESS, bal_engine_init(&engine, cpu, &allocator, &memory_interface));

engine.flags |= BAL_ENGINE_FLAG_SINGLE_STEP;

const bal_error_t run_status = bal_engine_run_thread(&engine);

bal_engine_destroy(&engine);
(void)bal_flat_translation_interface_destroy(&allocator, &memory_interface);

TEST_ASSERT_EQUAL(BAL_SUCCESS, run_status);
}

static void
test_BalTier1AddSub_AddsLeftShiftedSecondSource(void)
{
bal_cpu_t cpu = {};
cpu.x[8] = 0x0000000000000000ULL;
cpu.x[9] = 0x0000000000001000ULL;
cpu.x[10] = 0x00000000100000FFULL;

run_single_guest_instruction(ADD_X8_X9_X10_LSL4, &cpu);

TEST_ASSERT_EQUAL_HEX64(0x0000000100001FF0ULL, cpu.x[8]);
TEST_ASSERT_EQUAL_HEX64(0x00000000100000FFULL, cpu.x[10]);
}

static void
test_BalTier1AddSub_AddsLogicalRightShiftedSecondSource(void)
{
bal_cpu_t cpu = {};
cpu.x[9] = 0x0000000000000001ULL;
cpu.x[10] = 0x0000000000000FF0ULL;

run_single_guest_instruction(ADD_X8_X9_X10_LSR4, &cpu);

TEST_ASSERT_EQUAL_HEX64(0x0000000000000100ULL, cpu.x[8]);
}

static void
test_BalTier1AddSub_AddsArithmeticShiftedSecondSource(void)
{
bal_cpu_t cpu = {};
cpu.x[9] = 0x0000000000000100ULL;
cpu.x[10] = 0xFFFFFFFFFFFFFFF0ULL;

run_single_guest_instruction(ADD_X8_X9_X10_ASR4, &cpu);

TEST_ASSERT_EQUAL_HEX64(0x00000000000000FFULL, cpu.x[8]);
}

static void
test_BalTier1AddSub_AddsLeftShiftedBySixtyThree(void)
{
bal_cpu_t cpu = {};
cpu.x[9] = 0x0000000000000001ULL;
cpu.x[10] = 0x0000000000000001ULL;

run_single_guest_instruction(ADD_X8_X9_X10_LSL63, &cpu);

TEST_ASSERT_EQUAL_HEX64(0x8000000000000001ULL, cpu.x[8]);
}

static void
test_BalTier1AddSub_DestinationMatchingSecondSourceUsesOldValue(void)
{
bal_cpu_t cpu = {};
cpu.x[8] = 0x0000000000000001ULL;
cpu.x[9] = 0x0000000000000010ULL;

run_single_guest_instruction(ADD_X8_X9_X8_LSL4, &cpu);

TEST_ASSERT_EQUAL_HEX64(0x0000000000000020ULL, cpu.x[8]);
}

static void
test_BalTier1AddSub_DestinationMatchingFirstSource(void)
{
bal_cpu_t cpu = {};
cpu.x[8] = 0x0000000000000010ULL;
cpu.x[10] = 0x0000000000000001ULL;

run_single_guest_instruction(ADD_X8_X8_X10_LSL4, &cpu);

TEST_ASSERT_EQUAL_HEX64(0x0000000000000020ULL, cpu.x[8]);
}

static void
test_BalTier1AddSub_SubtractsLeftShiftedSecondSource(void)
{
bal_cpu_t cpu = {};
cpu.x[9] = 0x0000000000001000ULL;
cpu.x[10] = 0x00000000000000FFULL;

run_single_guest_instruction(SUB_X8_X9_X10_LSL4, &cpu);

TEST_ASSERT_EQUAL_HEX64(0x0000000000000010ULL, cpu.x[8]);
}

static void
test_BalTier1AddSub_SubtractionDestinationMatchingSecondSourceUsesOldValue(void)
{
bal_cpu_t cpu = {};
cpu.x[8] = 0x0000000000000010ULL;
cpu.x[9] = 0x0000000000000120ULL;

run_single_guest_instruction(SUB_X8_X9_X8_LSL4, &cpu);

TEST_ASSERT_EQUAL_HEX64(0x0000000000000020ULL, cpu.x[8]);
}

static void
test_BalTier1AddSub_CompareSetsFlagsFromShiftedOperand(void)
{
bal_cpu_t cpu = {};
cpu.x[8] = 0xDEADBEEFDEADBEEFULL;
cpu.x[9] = 0x0000000000000010ULL;
cpu.x[10] = 0x0000000000000001ULL;
cpu.x[31] = 0xCAFE000000000000ULL;

run_single_guest_instruction(CMP_X8_X9_LSL4, &cpu);

TEST_ASSERT_EQUAL_UINT8(0U, cpu.flag_n);
TEST_ASSERT_EQUAL_UINT8(1U, cpu.flag_z);
TEST_ASSERT_EQUAL_UINT8(1U, cpu.flag_c);
TEST_ASSERT_EQUAL_UINT8(0U, cpu.flag_v);

TEST_ASSERT_EQUAL_HEX64(0xDEADBEEFDEADBEEFULL, cpu.x[8]);
TEST_ASSERT_EQUAL_HEX64(0x0000000000000010ULL, cpu.x[9]);
TEST_ASSERT_EQUAL_HEX64(0x0000000000000001ULL, cpu.x[10]);
TEST_ASSERT_EQUAL_HEX64(0xCAFE000000000000ULL, cpu.x[31]);
}

static void
test_BalTier1AddSub_CompareNegateSetsFlagsFromShiftedOperand(void)
{
bal_cpu_t cpu = {};
cpu.x[8] = 0xDEADBEEFDEADBEEFULL;
cpu.x[9] = 0xFFFFFFFFFFFFFFF0ULL;
cpu.x[10] = 0xFFFFFFFFFFFFFFF0ULL;
cpu.x[31] = 0xCAFE000000000000ULL;

run_single_guest_instruction(CMN_X8_X9_ASR4, &cpu);

TEST_ASSERT_EQUAL_UINT8(1U, cpu.flag_n);
TEST_ASSERT_EQUAL_UINT8(0U, cpu.flag_z);
TEST_ASSERT_EQUAL_UINT8(1U, cpu.flag_c);
TEST_ASSERT_EQUAL_UINT8(0U, cpu.flag_v);

TEST_ASSERT_EQUAL_HEX64(0xDEADBEEFDEADBEEFULL, cpu.x[8]);
TEST_ASSERT_EQUAL_HEX64(0xFFFFFFFFFFFFFFF0ULL, cpu.x[9]);
TEST_ASSERT_EQUAL_HEX64(0xFFFFFFFFFFFFFFF0ULL, cpu.x[10]);
TEST_ASSERT_EQUAL_HEX64(0xCAFE000000000000ULL, cpu.x[31]);
}

static void
test_BalTier1AddSub_UnshiftedAddStillWorks(void)
{
bal_cpu_t cpu = {};
cpu.x[9] = 0x0000000000000010ULL;
cpu.x[10] = 0x0000000000000002ULL;

run_single_guest_instruction(ADD_X8_X9_X10, &cpu);

TEST_ASSERT_EQUAL_HEX64(0x0000000000000012ULL, cpu.x[8]);
}

static void
test_BalTier1AddSub_UnshiftedCompareStillWorks(void)
{
bal_cpu_t cpu = {};
cpu.x[8] = 0xDEADBEEFDEADBEEFULL;
cpu.x[9] = 0x0000000000000010ULL;
cpu.x[10] = 0x0000000000000020ULL;
cpu.x[31] = 0xCAFE000000000000ULL;

run_single_guest_instruction(CMP_X8_X9, &cpu);

TEST_ASSERT_EQUAL_UINT8(1U, cpu.flag_n);
TEST_ASSERT_EQUAL_UINT8(0U, cpu.flag_z);
TEST_ASSERT_EQUAL_UINT8(0U, cpu.flag_c);
TEST_ASSERT_EQUAL_UINT8(0U, cpu.flag_v);

TEST_ASSERT_EQUAL_HEX64(0xDEADBEEFDEADBEEFULL, cpu.x[8]);
TEST_ASSERT_EQUAL_HEX64(0x0000000000000010ULL, cpu.x[9]);
TEST_ASSERT_EQUAL_HEX64(0xCAFE000000000000ULL, cpu.x[31]);
}

int
main(void)
{
UNITY_BEGIN();

RUN_TEST(test_BalTier1AddSub_AddsLeftShiftedSecondSource);
RUN_TEST(test_BalTier1AddSub_AddsLogicalRightShiftedSecondSource);
RUN_TEST(test_BalTier1AddSub_AddsArithmeticShiftedSecondSource);
RUN_TEST(test_BalTier1AddSub_AddsLeftShiftedBySixtyThree);
RUN_TEST(test_BalTier1AddSub_DestinationMatchingSecondSourceUsesOldValue);
RUN_TEST(test_BalTier1AddSub_DestinationMatchingFirstSource);
RUN_TEST(test_BalTier1AddSub_SubtractsLeftShiftedSecondSource);
RUN_TEST(test_BalTier1AddSub_SubtractionDestinationMatchingSecondSourceUsesOldValue);
RUN_TEST(test_BalTier1AddSub_CompareSetsFlagsFromShiftedOperand);
RUN_TEST(test_BalTier1AddSub_CompareNegateSetsFlagsFromShiftedOperand);
RUN_TEST(test_BalTier1AddSub_UnshiftedAddStillWorks);
RUN_TEST(test_BalTier1AddSub_UnshiftedCompareStillWorks);

return UNITY_END();
}