From fd00640f6945a09f183a3b244f962142bf0746fb Mon Sep 17 00:00:00 2001 From: xolatile Date: Wed, 3 Jul 2024 01:28:01 -0400 Subject: [PATCH] Tested assembler generation... --- source/assembler.c | 208 ++++++++++++++++++++++++--------------------- 1 file changed, 112 insertions(+), 96 deletions(-) diff --git a/source/assembler.c b/source/assembler.c index 3453367..b8a66a1 100644 --- a/source/assembler.c +++ b/source/assembler.c @@ -1,122 +1,138 @@ #include "assembler.h" -static next byte_count = 0; -static byte * byte_array = NULL; +static next token_count = 0; +static byte * token_array = NULL; -static void byte_push (byte data) { - byte_array [byte_count] = data; +static void token_print (byte data) { + token_array [token_count] = data; - byte_count += 1; + token_count += 1; } -static inline form valid (form data) { return ((data => 0) && (data <= 15)); } -static inline form lower (form data) { return ((data => 0) && (data <= 7)); } -static inline form upper (form data) { return ((data => 8) && (data <= 15)); } +static inline form valid (form data) { return ((data >= 0) && (data <= 15)); } +static inline form lower (form data) { return ((data >= 0) && (data <= 7)); } +static inline form upper (form data) { return ((data >= 8) && (data <= 15)); } -//~static inline void format_prefix (size_index size, - //~type_index destination, - //~type_index source) { - //~if (size == SIZE_16B) { - //~byte_push (0X66); - //~} else if (size == SIZE_64B) { - //~byte format = 0X48; - //~if (destination == TYPE_REGISTER) { - //~format += upper (destination) * 0X01; - //~} - //~if (source == TYPE_REGISTER) { - //~format += upper (source) * 0X04; - //~} - //~byte_push (format); - //~} else { - //~return; - //~} -//~} - -//~static inline void format_rex_prefix (size_index size, - //~type_index destination, - //~type_index source) { - //~byte format = 0X40; - - //~byte_push (format); -//~} - -static inline void format_register_direction (size_index size, - form to, - form from) { - byte format = 0XC0; - - error (valid (to), "Invalid destination register index."); - error (valid (from), "Invalid source register index."); - - format += 1 * (to % 8); - format += 8 * (from % 8); - - byte_push (format); +static inline +void format_short_prefix (void) { + token_print (0X66); } -static inline void format_register_indirection (size_index size, - form to) { +static inline +void format_long_prefix (size_index size, + form destination, + form source) { + token_print (0X40 + + 0X01 * (upper (destination)) + + 0X02 * 0 + // UNDEFINED + 0X04 * (upper (source)) + + 0X08 * (size == SIZE_64B)); +} + +static inline +void format_register_direction (size_index size, + form destination, + form source) { + token_print (0XC0 + + 0X01 * (destination % 8) + + 0X08 * (source % 8)); +} +/* +static inline +void format_register_indirection (size_index size, + form destination) { byte format = 0XF0; - error (valid (to), "Invalid destination register index."); + format += destination % 8; - format += to % 8; - - byte_push (format); + token_print (format); +} +*/ +static inline +void format_register_redirection (size_index size, + form direction) { + token_print (0X05 + + 0X08 * (direction % 8)); } -static inline void format_register_redirection (size_index size, - form direction) { - byte format = 0X05; - - error (valid (direction), "Invalid direction register index."); - - format += 8 * (direction % 8); - - byte_push (format); +static inline +void format_constant (size_index size) { + token_print (0X80 + + 0X01 * (size == SIZE_8B)); } -//~static inline void format_operation (size_index size, - //~type_index type, - //~form destination, - //~form source) { - //~byte format = 0X00; +static inline +void format_regular_instruction (byte format, + size_index size, + type_index to, + form destination, + type_index from, + form source) { + //~format += 0X01 * (size != SIZE_8B); + //~format += 0X02 * ((to == TYPE_REGISTER) && (from == TYPE_VARIABLE)); + //~format += 0XC0 * ((to == TYPE_REGISTER) && (from == TYPE_CONSTANT)); + //~format += 0X04 * ((to == TYPE_VARIABLE) && (from == TYPE_CONSTANT)); - //~switch (type) { - //~case type_register_register: format += 0X30; break; - //~case type_register_variable: format += 0X32; break; - //~case type_register_constant: format += 0X80; break; - //~case type_variable_register: format += 0X30; break; - //~case type_variable_constant: format += 0X80; break; - //~default: exit (EXIT_FAILURE); + //~if ((to == TYPE_REGISTER) && (from == TYPE_CONSTANT)) { + //~format += destination % 8; //~} - //~if (size != size_8b) format += 0X01; + //~token_print (format); - //~if (type == type_register_constant) format -= (destination == 0) * 0X3A; + token_print (format + + destination % 8 * ((to == TYPE_REGISTER) && (from == TYPE_CONSTANT)) + + 0X01 * (size != SIZE_8B) + + 0X02 * ((to == TYPE_REGISTER) && (from == TYPE_VARIABLE)) + + 0XC0 * ((to == TYPE_REGISTER) && (from == TYPE_CONSTANT)) + + 0X04 * ((to == TYPE_VARIABLE) && (from == TYPE_CONSTANT))); +} - //~byte_push (format); -//~} +static inline +void assemble (operation_index operation, + size_index size, + type_index to, + form destination, + type_index from, + form source) { + byte code = 0X00; -//~static inline void format_modifier (size_index size, - //~type_index type, - //~form destination, - //~form source) { - //~byte format = 0X00; + if (size == SIZE_16B) { + format_short_prefix (); + } - //~byte_push (format); -//~} + if ((size == SIZE_64B) + || ((to == TYPE_REGISTER) && (upper (destination))) + || ((from == TYPE_REGISTER) && (upper (source)))) { + format_long_prefix (size, destination, source); + } -//~static void assemble_xor (size_index size, - //~type_index type, - //~form destination, - //~form source) { - //~if (size == size_256b) exit (EXIT_FAILURE); - //~if (size == size_128b) exit (EXIT_FAILURE); + if (from == TYPE_CONSTANT) { + format_constant (size); + } - //~format_prefix (size, type, destination, source); - //~format_operation (size, type, destination, source); - //~format_modifier (size, type, destination, source); + switch (operation) { + case OPERATION_ADD: code = 0X00; break; + case OPERATION_OR: code = 0X08; break; + case OPERATION_ADD_F: code = 0X10; break; + case OPERATION_SUBTRACT_F: code = 0X18; break; + case OPERATION_AND: code = 0X20; break; + case OPERATION_SUBTRACT: code = 0X28; break; + case OPERATION_XOR: code = 0X30; break; + case OPERATION_COMPARE: code = 0X38; break; + default: break; + } - //~// ... -//~} + format_regular_instruction (code, size, to, destination, from, source); + + if ((to == TYPE_REGISTER) && (from == TYPE_REGISTER)) { + format_register_direction (size, destination, source); + } + + if ((to == TYPE_REGISTER) && (from == TYPE_VARIABLE)) { + format_register_redirection (size, destination); + } + + if ((to == TYPE_VARIABLE) && (from == TYPE_REGISTER)) { + format_register_redirection (size, source); + } +}