From bdeafcbcc6bf2996c9834dc3519b370444d7a453 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Sat, 15 Jun 2024 03:03:02 +0100 Subject: [PATCH] Update the assembler file structure into subfolders --- src/assemble.c | 8 ++--- src/{ => assembler}/encode.c | 50 +++---------------------------- src/assembler/encode.h | 21 +++++++++++++ src/{ => assembler}/parser.c | 5 ++-- src/{ => assembler}/parser.h | 2 +- src/{ => assembler}/string_util.c | 2 +- src/{ => assembler}/string_util.h | 0 src/{ => assembler}/symboltable.c | 2 +- src/{ => assembler}/symboltable.h | 5 ++++ src/{ => assembler}/tokeniser.c | 0 src/{ => assembler}/tokeniser.h | 0 src/util/binary_util.c | 43 ++++++++++++++++++++++++++ src/util/binary_util.h | 17 +++++++++++ src/{ => util}/fileio.c | 2 +- src/{ => util}/fileio.h | 2 +- 15 files changed, 102 insertions(+), 57 deletions(-) rename src/{ => assembler}/encode.c (80%) create mode 100644 src/assembler/encode.h rename src/{ => assembler}/parser.c (99%) rename src/{ => assembler}/parser.h (92%) rename src/{ => assembler}/string_util.c (99%) rename src/{ => assembler}/string_util.h (100%) rename src/{ => assembler}/symboltable.c (99%) rename src/{ => assembler}/symboltable.h (97%) rename src/{ => assembler}/tokeniser.c (100%) rename src/{ => assembler}/tokeniser.h (100%) create mode 100644 src/util/binary_util.c create mode 100644 src/util/binary_util.h rename src/{ => util}/fileio.c (99%) rename src/{ => util}/fileio.h (92%) diff --git a/src/assemble.c b/src/assemble.c index 9b2484c..e159720 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -7,10 +7,10 @@ #include #include #include "a64instruction/a64instruction.h" -#include "parser.h" -#include "fileio.h" -#include "parser.h" -#include "encode.c" +#include "assembler/parser.h" +#include "util/fileio.h" +#include "assembler/encode.h" +#include "assembler/symboltable.h" static symbol_table *firstPass(a64inst_instruction *instructions, int lineCount); diff --git a/src/encode.c b/src/assembler/encode.c similarity index 80% rename from src/encode.c rename to src/assembler/encode.c index bdb89f4..ef7c498 100644 --- a/src/encode.c +++ b/src/assembler/encode.c @@ -7,55 +7,13 @@ * @author Saleh Bubshait */ -#include -#include "global.h" -#include "a64instruction/a64instruction.h" -#include "symboltable.c" +#include "symboltable.h" #include +#include "../util/binary_util.h" +#include "encode.h" #define HALT_BINARY 2315255808 -// write the provided value to the bits in the range [lsb, msb) {inclusive, exclusive} to the word. -// Does not modify any other bits in the word. -void setBits(word* wrd, uint8_t lsb, uint8_t msb, word value) { - // Ensure LSB and MSB are within range of word size, and in the correct order - assert(lsb < msb && msb <= 32); - - // Create a mask with 1s in the range [lsb, msb) and 0s elsewhere - word mask = 0; - for (uint8_t i = lsb; i < msb; i++) { - mask |= 1 << i; - } - - // Clear the bits in the range [lsb, msb) in the word - *wrd &= ~mask; - - // Set the bits in the range [lsb, msb) to the value - *wrd |= (value << lsb) & mask; -} - -// Sign extend a given value to a 64-bit signed integer given the number of bits -int64_t signExtend(dword value, unsigned int n) { - if (n == 0 || n >= 64) { - // If n_bits is 0 or greater than or equal to 64, return the value as is - return (int64_t)value; - } - - uint64_t sign_bit_mask = (uint64_t)1 << (n - 1); - - // Mask to isolate the n-bit value - uint64_t n_bit_mask = (sign_bit_mask << 1) - 1; - - // Check if the sign bit is set - if (value & sign_bit_mask) { - // Sign bit is set, extend the sign - return (int64_t)(value | ~n_bit_mask); - } else { - // Sign bit is not set, return the value as is - return (int64_t)(value & n_bit_mask); - } -} - static int getLabelOffset(symbol_table* table, char* label, int currentIndex, int n_bits) { address target = st_get(table, label); return signExtend((unsigned int) (target - currentIndex), n_bits); @@ -198,7 +156,7 @@ static word encodeLoadLiteral(a64inst_instruction cI, int arrIndex, symbol_table return wrd; } -static word *encode(a64inst_instruction insts[], int instCount, symbol_table* st) { +word *encode(a64inst_instruction insts[], int instCount, symbol_table* st) { word *arr = (word*)malloc(sizeof(word) * instCount); int index = 0; for (int i = 0; i < instCount; i++) { diff --git a/src/assembler/encode.h b/src/assembler/encode.h new file mode 100644 index 0000000..1ac8a82 --- /dev/null +++ b/src/assembler/encode.h @@ -0,0 +1,21 @@ +/** @file encode.h + * @brief A function to encode the internal representation of ARMv8 + * instructions, a64inst_instruction, into binary. + * + * @author Saleh Bubshait + */ + +#include "../global.h" +#include "../a64instruction/a64instruction.h" +#include "symboltable.h" + +/** @brief Encodes the internal representation of ARMv8 instructions into binary. + * The symbol table is used to resolve labels in branch instructions. Assumes + * that the instructions are in the same order as they appear in the source file. + * + * @param insts An array of a64inst_instruction to encode. + * @param instCount The number of instructions in the array. + * @param st The symbol table to use for label resolution. + * @return An array of words representing the binary encoding of the instructions. + */ +word *encode(a64inst_instruction insts[], int instCount, symbol_table* st); diff --git a/src/parser.c b/src/assembler/parser.c similarity index 99% rename from src/parser.c rename to src/assembler/parser.c index 474652e..b997cf3 100644 --- a/src/parser.c +++ b/src/assembler/parser.c @@ -1,6 +1,7 @@ /** @file parser.c * @brief Functions to parse ARMv8 assembly lines into an array of a special * internal representation of instructions, a64inst_instruction. + * * @author Ethan Dias Alberto * @author George Niedringhaus * @author Saleh Bubshait @@ -13,8 +14,8 @@ #include #include #include "parser.h" -#include "a64instruction/a64instruction.h" -#include "global.h" +#include "../a64instruction/a64instruction.h" +#include "../global.h" #include "tokeniser.h" #include "string_util.h" diff --git a/src/parser.h b/src/assembler/parser.h similarity index 92% rename from src/parser.h rename to src/assembler/parser.h index 23b76c6..12d5f7b 100644 --- a/src/parser.h +++ b/src/assembler/parser.h @@ -6,7 +6,7 @@ * @author Saleh Bubshait */ -#include "a64instruction/a64instruction.h" +#include "../a64instruction/a64instruction.h" /** @brief Parses a list of ARMv8 assembly lines into an array of a64inst_instruction. * diff --git a/src/string_util.c b/src/assembler/string_util.c similarity index 99% rename from src/string_util.c rename to src/assembler/string_util.c index 8b7aaa0..e519ef4 100644 --- a/src/string_util.c +++ b/src/assembler/string_util.c @@ -10,7 +10,7 @@ #include #include #include "string_util.h" -#include "global.h" +#include "../global.h" /************************************ * CONSTANTS diff --git a/src/string_util.h b/src/assembler/string_util.h similarity index 100% rename from src/string_util.h rename to src/assembler/string_util.h diff --git a/src/symboltable.c b/src/assembler/symboltable.c similarity index 99% rename from src/symboltable.c rename to src/assembler/symboltable.c index 50db150..9ccd3d6 100644 --- a/src/symboltable.c +++ b/src/assembler/symboltable.c @@ -5,7 +5,7 @@ * * @author Saleh Bubshait */ - + #include #include #include diff --git a/src/symboltable.h b/src/assembler/symboltable.h similarity index 97% rename from src/symboltable.h rename to src/assembler/symboltable.h index ca1912d..12c99b8 100644 --- a/src/symboltable.h +++ b/src/assembler/symboltable.h @@ -6,6 +6,9 @@ * @author Saleh Bubshait */ +#ifndef __SYMBOLTABLE__ +#define __SYMBOLTABLE__ + #include #include #include @@ -68,3 +71,5 @@ address st_get(symbol_table *st, char *label); * @param st A pointer to the target symbol table. */ void st_free(symbol_table *st); + +#endif diff --git a/src/tokeniser.c b/src/assembler/tokeniser.c similarity index 100% rename from src/tokeniser.c rename to src/assembler/tokeniser.c diff --git a/src/tokeniser.h b/src/assembler/tokeniser.h similarity index 100% rename from src/tokeniser.h rename to src/assembler/tokeniser.h diff --git a/src/util/binary_util.c b/src/util/binary_util.c new file mode 100644 index 0000000..af85377 --- /dev/null +++ b/src/util/binary_util.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include "binary_util.h" + +void setBits(word* wrd, uint8_t lsb, uint8_t msb, word value) { + // Ensure LSB and MSB are within range of word size, and in the correct order + assert(lsb < msb && msb <= 32); + + // Create a mask with 1s in the range [lsb, msb) and 0s elsewhere + word mask = 0; + for (uint8_t i = lsb; i < msb; i++) { + mask |= 1 << i; + } + + // Clear the bits in the range [lsb, msb) in the word + *wrd &= ~mask; + + // Set the bits in the range [lsb, msb) to the value + *wrd |= (value << lsb) & mask; +} + +// Sign extend a given value to a 64-bit signed integer given the number of bits +int64_t signExtend(dword value, unsigned int n) { + if (n == 0 || n >= 64) { + // If n_bits is 0 or greater than or equal to 64, return the value as is + return (int64_t)value; + } + + uint64_t sign_bit_mask = (uint64_t)1 << (n - 1); + + // Mask to isolate the n-bit value + uint64_t n_bit_mask = (sign_bit_mask << 1) - 1; + + // Check if the sign bit is set + if (value & sign_bit_mask) { + // Sign bit is set, extend the sign + return (int64_t)(value | ~n_bit_mask); + } else { + // Sign bit is not set, return the value as is + return (int64_t)(value & n_bit_mask); + } +} diff --git a/src/util/binary_util.h b/src/util/binary_util.h new file mode 100644 index 0000000..348dda2 --- /dev/null +++ b/src/util/binary_util.h @@ -0,0 +1,17 @@ +/** + */ + +#include "../global.h" + +/** @brief Sets a range of bits of a word (32-bit unsigned integer) to a value. + * The range is inclusive of the lsb and exclusive of the msb. The value should + * fit within the range. + * + * @param wrd A pointer to the word to set bits in. + * @param lsb The least significant bit of the range to set, inclusive. + * @param msb The most significant bit of the range to set, exclusive. + * @param value The value to set the bits to. + */ +void setBits(word* wrd, uint8_t lsb, uint8_t msb, word value); + +int64_t signExtend(dword value, unsigned int n); diff --git a/src/fileio.c b/src/util/fileio.c similarity index 99% rename from src/fileio.c rename to src/util/fileio.c index cd4fcc6..597cbcf 100644 --- a/src/fileio.c +++ b/src/util/fileio.c @@ -1,5 +1,5 @@ #include -#include "global.h" +#include "../global.h" #include "fileio.h" #define MAX_ASM_LINE_LENGTH 300 diff --git a/src/fileio.h b/src/util/fileio.h similarity index 92% rename from src/fileio.h rename to src/util/fileio.h index 3a509ca..b3c9cbc 100644 --- a/src/fileio.h +++ b/src/util/fileio.h @@ -2,7 +2,7 @@ #define __FILEIO__ #include #include -#include "global.h" +#include "../global.h" #define EXIT_FAILURE 1