Update the assembler file structure into subfolders
This commit is contained in:
parent
6de1915dbe
commit
bdeafcbcc6
@ -7,10 +7,10 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "a64instruction/a64instruction.h"
|
#include "a64instruction/a64instruction.h"
|
||||||
#include "parser.h"
|
#include "assembler/parser.h"
|
||||||
#include "fileio.h"
|
#include "util/fileio.h"
|
||||||
#include "parser.h"
|
#include "assembler/encode.h"
|
||||||
#include "encode.c"
|
#include "assembler/symboltable.h"
|
||||||
|
|
||||||
static symbol_table *firstPass(a64inst_instruction *instructions, int lineCount);
|
static symbol_table *firstPass(a64inst_instruction *instructions, int lineCount);
|
||||||
|
|
||||||
|
|||||||
@ -7,55 +7,13 @@
|
|||||||
* @author Saleh Bubshait
|
* @author Saleh Bubshait
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <assert.h>
|
#include "symboltable.h"
|
||||||
#include "global.h"
|
|
||||||
#include "a64instruction/a64instruction.h"
|
|
||||||
#include "symboltable.c"
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include "../util/binary_util.h"
|
||||||
|
#include "encode.h"
|
||||||
|
|
||||||
#define HALT_BINARY 2315255808
|
#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) {
|
static int getLabelOffset(symbol_table* table, char* label, int currentIndex, int n_bits) {
|
||||||
address target = st_get(table, label);
|
address target = st_get(table, label);
|
||||||
return signExtend((unsigned int) (target - currentIndex), n_bits);
|
return signExtend((unsigned int) (target - currentIndex), n_bits);
|
||||||
@ -198,7 +156,7 @@ static word encodeLoadLiteral(a64inst_instruction cI, int arrIndex, symbol_table
|
|||||||
return wrd;
|
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);
|
word *arr = (word*)malloc(sizeof(word) * instCount);
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (int i = 0; i < instCount; i++) {
|
for (int i = 0; i < instCount; i++) {
|
||||||
21
src/assembler/encode.h
Normal file
21
src/assembler/encode.h
Normal file
@ -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);
|
||||||
@ -1,6 +1,7 @@
|
|||||||
/** @file parser.c
|
/** @file parser.c
|
||||||
* @brief Functions to parse ARMv8 assembly lines into an array of a special
|
* @brief Functions to parse ARMv8 assembly lines into an array of a special
|
||||||
* internal representation of instructions, a64inst_instruction.
|
* internal representation of instructions, a64inst_instruction.
|
||||||
|
*
|
||||||
* @author Ethan Dias Alberto
|
* @author Ethan Dias Alberto
|
||||||
* @author George Niedringhaus
|
* @author George Niedringhaus
|
||||||
* @author Saleh Bubshait
|
* @author Saleh Bubshait
|
||||||
@ -13,8 +14,8 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "a64instruction/a64instruction.h"
|
#include "../a64instruction/a64instruction.h"
|
||||||
#include "global.h"
|
#include "../global.h"
|
||||||
#include "tokeniser.h"
|
#include "tokeniser.h"
|
||||||
#include "string_util.h"
|
#include "string_util.h"
|
||||||
|
|
||||||
@ -6,7 +6,7 @@
|
|||||||
* @author Saleh Bubshait
|
* @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.
|
/** @brief Parses a list of ARMv8 assembly lines into an array of a64inst_instruction.
|
||||||
*
|
*
|
||||||
@ -10,7 +10,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "string_util.h"
|
#include "string_util.h"
|
||||||
#include "global.h"
|
#include "../global.h"
|
||||||
|
|
||||||
/************************************
|
/************************************
|
||||||
* CONSTANTS
|
* CONSTANTS
|
||||||
@ -6,6 +6,9 @@
|
|||||||
* @author Saleh Bubshait
|
* @author Saleh Bubshait
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifndef __SYMBOLTABLE__
|
||||||
|
#define __SYMBOLTABLE__
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -68,3 +71,5 @@ address st_get(symbol_table *st, char *label);
|
|||||||
* @param st A pointer to the target symbol table.
|
* @param st A pointer to the target symbol table.
|
||||||
*/
|
*/
|
||||||
void st_free(symbol_table *st);
|
void st_free(symbol_table *st);
|
||||||
|
|
||||||
|
#endif
|
||||||
43
src/util/binary_util.c
Normal file
43
src/util/binary_util.c
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#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);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/util/binary_util.h
Normal file
17
src/util/binary_util.h
Normal file
@ -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);
|
||||||
@ -1,5 +1,5 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "global.h"
|
#include "../global.h"
|
||||||
#include "fileio.h"
|
#include "fileio.h"
|
||||||
|
|
||||||
#define MAX_ASM_LINE_LENGTH 300
|
#define MAX_ASM_LINE_LENGTH 300
|
||||||
@ -2,7 +2,7 @@
|
|||||||
#define __FILEIO__
|
#define __FILEIO__
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "global.h"
|
#include "../global.h"
|
||||||
|
|
||||||
#define EXIT_FAILURE 1
|
#define EXIT_FAILURE 1
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user