Refactored assembler to be branchless...

This commit is contained in:
xolatile
2024-07-30 10:54:47 -04:00
parent 617a65999e
commit 0fe697623c

View File

@ -3,22 +3,22 @@
#include <stdlib.h>
#define REGULAR_BEGIN (ADD)
#define REGULAR_END (CMP)
#define IRREGULAR_BEGIN (INC)
#define IRREGULAR_END (IDIV)
#define SPECIAL_1_BEGIN (NOP)
#define SPECIAL_1_END (PUSHF)
#define SPECIAL_2_BEGIN (SYSCALL)
#define SPECIAL_2_END (FCOS)
#define DOUBLE_BEGIN (ADD)
#define DOUBLE_END (CMP)
#define SINGLE_BEGIN (INC)
#define SINGLE_END (IDIV)
#define STATIC_1_BEGIN (NOP)
#define STATIC_1_END (PUSHF)
#define STATIC_2_BEGIN (SYSCALL)
#define STATIC_2_END (FCOS)
#define JUMP_IF_BEGIN (JO)
#define JUMP_IF_END (JG)
#define MOVE_IF_BEGIN (CMOVO)
#define MOVE_IF_END (CMOVG)
#define FLOAT_BEGIN (FADD)
#define FLOAT_END (FDIVR)
#define SHIFTER_BEGIN (ROL)
#define SHIFTER_END (SAR)
#define SHIFT_BEGIN (ROL)
#define SHIFT_END (SAR)
static const char * size_name [] = {
"\033[1;36md8 \033[0m", "\033[1;36md16\033[0m", "\033[1;36md32\033[0m", "\033[1;36md64\033[0m"
@ -64,8 +64,6 @@ static const char * data_name [] = {
"\033[1;33mrepnz\033[0m", "\033[1;33mloop\033[0m", "\033[1;33mloope\033[0m", "\033[1;33mloopne\033[0m"
};
static int assemble_clean_up_queued = 0;
static unsigned int empty_count = 1;
static unsigned int empty_holes = 1;
static unsigned int * empty_array = NULL;
@ -179,7 +177,7 @@ static void modify_memory (unsigned int operation, unsigned int to, unsigned int
}
// REFACTORING IN PROGRESS
static unsigned int build_regular (unsigned int * array) {
static unsigned int build_double (unsigned int * array) {
unsigned int operation = array [0],
size = array [1],
to = array [2],
@ -199,7 +197,7 @@ static unsigned int build_regular (unsigned int * array) {
inset ((from == IMM) && (to == REG) && (destination == 0), 0x05 + 0x08 * (operation & 0x07) - 0x01 * (size == D8));
inset (! ((from == IMM) && (to == REG) && (destination == 0)), (destination & 0x07) * ((to == REG) && (from == IMM)) + 0x08 * (operation - REGULAR_BEGIN) + 0x01 * ((to == MEM) && (from == IMM) && (size == D8)) - 0x01 * ((to == REG) && (from == IMM) && (size != D8)) + 0x01 * (size != D8) + 0x02 * ((to == REG) && (from == MEM)) + 0x04 * ((to == MEM) && (from == IMM)) + 0xc0 * ((to == REG) && (from == IMM)));
inset (! ((from == IMM) && (to == REG) && (destination == 0)), (destination & 0x07) * ((to == REG) && (from == IMM)) + 0x08 * (operation - DOUBLE_BEGIN) + 0x01 * ((to == MEM) && (from == IMM) && (size == D8)) - 0x01 * ((to == REG) && (from == IMM) && (size != D8)) + 0x01 * (size != D8) + 0x02 * ((to == REG) && (from == MEM)) + 0x04 * ((to == MEM) && (from == IMM)) + 0xc0 * ((to == REG) && (from == IMM)));
modify_registers (to, destination, from, source);
@ -216,7 +214,7 @@ static unsigned int build_regular (unsigned int * array) {
return (5);
}
static unsigned int build_irregular (unsigned int * array) {
static unsigned int build_single (unsigned int * array) {
unsigned int operation = array [0],
size = array [1],
to = array [2],
@ -232,15 +230,16 @@ static unsigned int build_irregular (unsigned int * array) {
inset (1, 0xf7 + 0x08 * ((operation == INC) || (operation == DEC)) - 0x01 * (size == D8));
inset (to == REG, 0xc0 + 0x08 * (operation - IRREGULAR_BEGIN) + 0x01 * (destination & 0x07));
inset (to == MEM, 0x05 + 0x08 * (operation - IRREGULAR_BEGIN));
// THIS CAN BE REFACTORED TO C0F8 AND 053D
inset (to == REG, 0xc0 + 0x08 * (operation - SINGLE_BEGIN) + 0x01 * (destination & 0x07));
inset (to == MEM, 0x05 + 0x08 * (operation - SINGLE_BEGIN));
inset_memory (to == MEM, D32, destination, 0x1000 - (text_sector_size + 4));
return (3);
}
static unsigned int build_special_1 (unsigned int * array) {
static unsigned int build_static_1 (unsigned int * array) {
unsigned int operation = array [0];
const unsigned char data [] = {
@ -249,12 +248,12 @@ static unsigned int build_special_1 (unsigned int * array) {
debug_printf ("> %s", data_name [array [0]]);
inset (1, data [operation - SPECIAL_1_BEGIN]);
inset (1, data [operation - STATIC_1_BEGIN]);
return (0);
}
static unsigned int build_special_2 (unsigned int * array) {
static unsigned int build_static_2 (unsigned int * array) {
unsigned int operation = array [0];
const unsigned short data [] = {
@ -266,7 +265,7 @@ static unsigned int build_special_2 (unsigned int * array) {
debug_printf ("> %s", data_name [array [0]]);
inset_immediate (1, D16, data [operation - SPECIAL_2_BEGIN]);
inset_immediate (1, D16, data [operation - STATIC_2_BEGIN]);
return (0);
}
@ -423,7 +422,7 @@ static unsigned int build_float (unsigned int * array) {
return (3);
}
static unsigned int build_shifter (unsigned int * array) {
static unsigned int build_shift (unsigned int * array) {
unsigned int operation = array [0],
size = array [1],
to = array [2],
@ -506,16 +505,32 @@ static unsigned int build_push (unsigned int * array) {
return (3);
}
static void assemble_clean_up (void) {
if (!assemble_clean_up_queued) {
return;
static unsigned int fault (unsigned int * array) {
debug_printf ("> \033[0;31m%u\033[0m", array [0]);
return (0);
}
free (text_sector_byte);
free (empty_array);
free (empty_imbue);
free (empty_store);
}
static const unsigned int (* build_instruction []) (unsigned int * array) = {
store_memory, store_relative, store_immediate,
fault, // ASMDIRREP IS UNIMPLEMENTED CURRENTLY
build_double, build_double, build_double, build_double, build_double, build_double, build_double, build_double,
build_single, build_single, build_single, build_single, build_single, build_single, build_single, build_single,
build_float, build_float, build_float, build_float, build_float, build_float, build_float, build_float,
build_shift, build_shift, build_shift, build_shift, build_shift, build_shift, build_shift, build_shift,
build_static_1, build_static_1, build_static_1, build_static_1, build_static_1, build_static_1,
build_static_2, build_static_2, build_static_2, build_static_2, build_static_2, build_static_2, build_static_2, build_static_2,
build_static_2, build_static_2, build_static_2, build_static_2, build_static_2, build_static_2, build_static_2, build_static_2,
build_static_2, build_static_2, build_static_2, build_static_2, build_static_2, build_static_2, build_static_2, build_static_2,
build_static_2, build_static_2, build_static_2, build_static_2, build_static_2, build_static_2,
build_enter, build_call, build_in_out, build_in_out, build_jump, build_move, build_pop, build_push,
build_jump_if, build_jump_if, build_jump_if, build_jump_if, build_jump_if, build_jump_if, build_jump_if, build_jump_if,
build_jump_if, build_jump_if, build_jump_if, build_jump_if, build_jump_if, build_jump_if, build_jump_if, build_jump_if,
build_move_if, build_move_if, build_move_if, build_move_if, build_move_if, build_move_if, build_move_if, build_move_if,
build_move_if, build_move_if, build_move_if, build_move_if, build_move_if, build_move_if, build_move_if, build_move_if,
fault, fault, fault, fault, fault, fault,
fault, fault, fault, fault, fault, fault, fault, fault
};
unsigned int nopification = 1;
unsigned int text_entry_point = 0;
@ -537,11 +552,6 @@ int assemble (unsigned int count, unsigned int * array) {
empty_imbue = calloc (1024UL, sizeof (* empty_imbue));
empty_store = calloc (1024UL, sizeof (* empty_store));
if (!assemble_clean_up_queued) {
atexit (assemble_clean_up);
assemble_clean_up_queued = ! assemble_clean_up_queued;
}
for (index = 0; index < count; ++index) {
unsigned int check_at;
unsigned int byte;
@ -550,36 +560,7 @@ int assemble (unsigned int count, unsigned int * array) {
check_at = text_sector_size;
if ((array [index] >= REGULAR_BEGIN) && (array [index] <= REGULAR_END)) {
index += build_regular (& array [index]);
} else if ((array [index] >= IRREGULAR_BEGIN) && (array [index] <= IRREGULAR_END)) {
index += build_irregular (& array [index]);
} else if ((array [index] >= SPECIAL_1_BEGIN) && (array [index] <= SPECIAL_1_END)) {
index += build_special_1 (& array [index]);
} else if ((array [index] >= SPECIAL_2_BEGIN) && (array [index] <= SPECIAL_2_END)) {
index += build_special_2 (& array [index]);
} else if ((array [index] >= JUMP_IF_BEGIN) && (array [index] <= JUMP_IF_END)) {
index += build_jump_if (& array [index]);
} else if ((array [index] >= MOVE_IF_BEGIN) && (array [index] <= MOVE_IF_END)) {
index += build_move_if (& array [index]);
} else if ((array [index] >= FLOAT_BEGIN) && (array [index] <= FLOAT_END)) {
index += build_float (& array [index]);
} else if ((array [index] >= SHIFTER_BEGIN) && (array [index] <= SHIFTER_END)) {
index += build_shifter (& array [index]);
} else if ((array [index] == IN) || (array [index] == OUT)) {
index += build_in_out (& array [index]);
} else switch (array [index]) {
case ASMDIRREL: index += store_relative (& array [index]); break;
case ASMDIRMEM: index += store_memory (& array [index]); break;
case ASMDIRIMM: index += store_immediate (& array [index]); break;
case JMP: index += build_jump (& array [index]); break;
case MOV: index += build_move (& array [index]); break;
case CALL: index += build_call (& array [index]); break;
case ENTER: index += build_enter (& array [index]); break;
case POP: index += build_pop (& array [index]); break;
case PUSH: index += build_push (& array [index]); break;
default: return (EXIT_FAILURE);
}
index += build_instruction [array [index]] (& array [index]);
debug_printf (" -- \033[0;35m");
@ -603,5 +584,10 @@ int assemble (unsigned int count, unsigned int * array) {
replace (& text_sector_byte [get], (unsigned char *) & set, sizeof (set));
}
return (EXIT_SUCCESS);
free (text_sector_byte);
free (empty_array);
free (empty_imbue);
free (empty_store);
return (0);
}