From 51ae5fcdb1dca9b5d2df3f417045701e35c0f282 Mon Sep 17 00:00:00 2001 From: Alvin Cheng Date: Sat, 21 Dec 2024 22:06:51 +1100 Subject: [PATCH] Changes to preprocessor functions (#17) * Removed redundant pre processor functions Some pre-processor function calls for instruction encoder identities such as `ZO`. Previously the `pre_default()` pre processor function was called to check if the operand sizes are the same; However in this special edge case where there are *no* operands, we can simply ignore this behaviour. * Renamed `pre_default` The previous name of `pre_default` seemed *too general* and infered that this is the default behavior of the assembler, however desc- ribing the pre processor function as `same_operand_sizes` more ac- curatly encapsulates the expected behavior instead of needing to skim over the code before adding it. * Broken the line for `syscall` for code readability --- libjas/instruction.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/libjas/instruction.c b/libjas/instruction.c index d113f1d..4aca3ad 100644 --- a/libjas/instruction.c +++ b/libjas/instruction.c @@ -27,7 +27,7 @@ #include "error.h" #include -static void pre_default(operand_t *op_arr, buffer_t *buf, instr_encode_table_t *instr_ref, enum modes mode) { +static void same_operand_sizes(operand_t *op_arr, buffer_t *buf, instr_encode_table_t *instr_ref, enum modes mode) { const uint8_t ref = op_sizeof(op_arr[0].type); for (uint8_t i = 0; i < 4; i++) { @@ -42,15 +42,15 @@ static void pre_default(operand_t *op_arr, buffer_t *buf, instr_encode_table_t * static void pre_imm(operand_t *op_arr, buffer_t *buf, instr_encode_table_t *instr_ref, enum modes mode) { if (op_sizeof(op_arr[0].type) != 64) { - pre_default(op_arr, buf, instr_ref, mode); + same_operand_sizes(op_arr, buf, instr_ref, mode); return; } } static instr_encode_table_t mov[] = { - {OP_MR, NULL, {0x89}, MODE_SUPPORT_ALL, {0x88}, 1, &pre_default}, - {OP_RM, NULL, {0x8B}, MODE_SUPPORT_ALL, {0x8A}, 1, &pre_default}, - {OP_OI, NULL, {0xB8}, MODE_SUPPORT_ALL, {0xB0}, 1, &pre_default}, + {OP_MR, NULL, {0x89}, MODE_SUPPORT_ALL, {0x88}, 1, &same_operand_sizes}, + {OP_RM, NULL, {0x8B}, MODE_SUPPORT_ALL, {0x8A}, 1, &same_operand_sizes}, + {OP_OI, NULL, {0xB8}, MODE_SUPPORT_ALL, {0xB0}, 1, &same_operand_sizes}, {OP_MI, 0b10000000, {0xC7}, MODE_SUPPORT_ALL, {0xC6}, 1, &pre_imm}, INSTR_TERMINATOR, @@ -67,8 +67,8 @@ static void pre_lea(operand_t *op_arr, buffer_t *buf, instr_encode_table_t *inst // clang-format off #define INSTR_GENERAL(rm, rm_byte, mr, mr_byte,i, i_byte, mi_ext, mi, mi_byte) \ - {OP_MR, NULL, {mr}, MODE_SUPPORT_ALL, {mr_byte}, 1, &pre_default}, \ - {OP_RM, NULL, {rm}, MODE_SUPPORT_ALL, {rm_byte}, 1, &pre_default}, \ + {OP_MR, NULL, {mr}, MODE_SUPPORT_ALL, {mr_byte}, 1, &same_operand_sizes}, \ + {OP_RM, NULL, {rm}, MODE_SUPPORT_ALL, {rm_byte}, 1, &same_operand_sizes}, \ {OP_MI, mi_ext, {mi}, MODE_SUPPORT_ALL, {mi_byte}, 1, &pre_imm}, \ {OP_I, NULL, {i}, MODE_SUPPORT_ALL, {i_byte}, 1, &pre_imm}, \ INSTR_TERMINATOR, @@ -79,8 +79,8 @@ static instr_encode_table_t lea[] = {{OP_RM, NULL, {0x8D}, MODE_SUPPORT_ALL, {0x static instr_encode_table_t add[] = {INSTR_GENERAL(0x03, 0x02, 0x01, 0x00, 0x03, 0x02, 0b10000000, 0x81, 0x80)}; static instr_encode_table_t sub[] = {INSTR_GENERAL(0x2B, 0x2A, 0x28, 0x29, 0x2C, 0x2D, 5, 0x81, 0x80)}; -static instr_encode_table_t mul[] = {{OP_M, 4, {0xF7}, MODE_SUPPORT_ALL, {0xF6}, 1, &pre_default}, INSTR_TERMINATOR}; -static instr_encode_table_t div[] = {{OP_M, 6, {0xF7}, MODE_SUPPORT_ALL, {0xF6}, 1, &pre_default}, INSTR_TERMINATOR}; +static instr_encode_table_t mul[] = {{OP_M, 4, {0xF7}, MODE_SUPPORT_ALL, {0xF6}, 1, &same_operand_sizes}, INSTR_TERMINATOR}; +static instr_encode_table_t div[] = {{OP_M, 6, {0xF7}, MODE_SUPPORT_ALL, {0xF6}, 1, &same_operand_sizes}, INSTR_TERMINATOR}; // Note all or, and and xor instructions have a imm8 which is not supported @@ -90,10 +90,10 @@ static instr_encode_table_t xor [] = {INSTR_GENERAL(0x33, 0x32, 0x31, 0x30, 0x35 // --- -static instr_encode_table_t _not[] = {{OP_M, 2, {0xF7}, MODE_SUPPORT_ALL, {0xF6}, 1, &pre_default}, INSTR_TERMINATOR}; +static instr_encode_table_t _not[] = {{OP_M, 2, {0xF7}, MODE_SUPPORT_ALL, {0xF6}, 1, &same_operand_sizes}, INSTR_TERMINATOR}; -static instr_encode_table_t inc[] = {{OP_M, 0, {0xFF}, MODE_SUPPORT_ALL, {0xFE}, 1, &pre_default}, INSTR_TERMINATOR}; -static instr_encode_table_t dec[] = {{OP_M, 1, {0xFF}, MODE_SUPPORT_ALL, {0xFE}, 1, &pre_default}, INSTR_TERMINATOR}; +static instr_encode_table_t inc[] = {{OP_M, 0, {0xFF}, MODE_SUPPORT_ALL, {0xFE}, 1, &same_operand_sizes}, INSTR_TERMINATOR}; +static instr_encode_table_t dec[] = {{OP_M, 1, {0xFF}, MODE_SUPPORT_ALL, {0xFE}, 1, &same_operand_sizes}, INSTR_TERMINATOR}; static void pre_jcc_no_byte(operand_t *op_arr, buffer_t *buf, instr_encode_table_t *instr_ref, enum modes mode) { if (op_sizeof(op_arr[0].type) == 8) @@ -124,7 +124,7 @@ static void pre_ret(operand_t *op_arr, buffer_t *buf, instr_encode_table_t *inst // TODO / note far jumps, calls and returns are not supported (yet) static instr_encode_table_t ret[] = { - {OP_ZO, NULL, {0xC3}, MODE_SUPPORT_ALL, {0xC3}, 1, &pre_default}, + {OP_ZO, NULL, {0xC3}, MODE_SUPPORT_ALL, {0xC3}, 1, NULL}, {OP_I, NULL, {0xC2}, MODE_SUPPORT_ALL, {0xC2}, 1, &pre_ret}, INSTR_TERMINATOR, }; @@ -147,13 +147,13 @@ static instr_encode_table_t pop[] = { static instr_encode_table_t in[] = {{}}; static instr_encode_table_t out[] = {{}}; -static instr_encode_table_t clc[] = {{OP_ZO, NULL, {0xF8}, MODE_SUPPORT_ALL, {0xF8}, 1, &pre_default}, INSTR_TERMINATOR}; -static instr_encode_table_t stc[] = {{OP_ZO, NULL, {0xF9}, MODE_SUPPORT_ALL, {0xF9}, 1, &pre_default}, INSTR_TERMINATOR}; -static instr_encode_table_t cli[] = {{OP_ZO, NULL, {0xFA}, MODE_SUPPORT_ALL, {0xFA}, 1, &pre_default}, INSTR_TERMINATOR}; -static instr_encode_table_t sti[] = {{OP_ZO, NULL, {0xFB}, MODE_SUPPORT_ALL, {0xFB}, 1, &pre_default}, INSTR_TERMINATOR}; +static instr_encode_table_t clc[] = {{OP_ZO, NULL, {0xF8}, MODE_SUPPORT_ALL, {0xF8}, 1, NULL}, INSTR_TERMINATOR}; +static instr_encode_table_t stc[] = {{OP_ZO, NULL, {0xF9}, MODE_SUPPORT_ALL, {0xF9}, 1, NULL}, INSTR_TERMINATOR}; +static instr_encode_table_t cli[] = {{OP_ZO, NULL, {0xFA}, MODE_SUPPORT_ALL, {0xFA}, 1, NULL}, INSTR_TERMINATOR}; +static instr_encode_table_t sti[] = {{OP_ZO, NULL, {0xFB}, MODE_SUPPORT_ALL, {0xFB}, 1, NULL}, INSTR_TERMINATOR}; -static instr_encode_table_t nop[] = {{OP_ZO, NULL, {0x90}, MODE_SUPPORT_ALL, {0x90}, 1, &pre_default}, INSTR_TERMINATOR}; -static instr_encode_table_t hlt[] = {{OP_ZO, NULL, {0xF4}, MODE_SUPPORT_ALL, {0xF4}, 1, &pre_default}, INSTR_TERMINATOR}; +static instr_encode_table_t nop[] = {{OP_ZO, NULL, {0x90}, MODE_SUPPORT_ALL, {0x90}, 1, NULL}, INSTR_TERMINATOR}; +static instr_encode_table_t hlt[] = {{OP_ZO, NULL, {0xF4}, MODE_SUPPORT_ALL, {0xF4}, 1, NULL}, INSTR_TERMINATOR}; static void pre_int(operand_t *op_arr, buffer_t *buf, instr_encode_table_t *instr_ref, enum modes mode) { if (op_sizeof(op_arr[0].type) != 8) @@ -161,7 +161,10 @@ static void pre_int(operand_t *op_arr, buffer_t *buf, instr_encode_table_t *inst } static instr_encode_table_t _int[] = {{OP_I, NULL, {0xCD}, MODE_SUPPORT_ALL, {0xCD}, 1, &pre_int}, INSTR_TERMINATOR}; -static instr_encode_table_t syscall[] = {{OP_ZO, NULL, {0x0F, 0x05}, MODE_SUPPORT_64BIT, {0x00, 0x00}, 2, &pre_default}, INSTR_TERMINATOR}; +static instr_encode_table_t syscall[] = { + {OP_ZO, NULL, {0x0F, 0x05}, MODE_SUPPORT_64BIT, {0x00, 0x00}, 2, &same_operand_sizes}, + INSTR_TERMINATOR, +}; // clang-format off