From 61f6fc9506a9348046ba15780fd5c63388713dd9 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Fri, 31 May 2024 15:42:35 +0100 Subject: [PATCH 1/9] create binary file write function --- src/fileaccess.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/fileaccess.c diff --git a/src/fileaccess.c b/src/fileaccess.c new file mode 100644 index 0000000..aec7195 --- /dev/null +++ b/src/fileaccess.c @@ -0,0 +1,14 @@ +#include + +void writeBinaryFile(word instrs[], char outputFile[]){ + + + FILE *fp; + + fp = fopen(outputFile, "wb"); + fwrite(instrs, 4, sizeof(instrs), fp); + fclose(fp); + +} + +void readAssemblyFile() \ No newline at end of file From fa17a7fda35e98f8284587d473ab907b0ef72290 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Fri, 31 May 2024 15:53:41 +0100 Subject: [PATCH 2/9] add inputted filename format checking --- src/fileaccess.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/fileaccess.c b/src/fileaccess.c index aec7195..e3e5df8 100644 --- a/src/fileaccess.c +++ b/src/fileaccess.c @@ -1,7 +1,22 @@ #include +#include -void writeBinaryFile(word instrs[], char outputFile[]){ +bool isValidFileFormat(char filename[], char expectedExtension[]){ + int *pointLoc = strrchr(filename, '.'); + if(pointLoc != NULL){ + if(strcmp(pointLoc, expectedExtension)==0){ + return true; + } + } + return false; +} + +int writeBinaryFile(word instrs[], char outputFile[]){ + + if (!isValidFileFormat(filename, "bin")){ + return(1); + } FILE *fp; @@ -9,6 +24,9 @@ void writeBinaryFile(word instrs[], char outputFile[]){ fwrite(instrs, 4, sizeof(instrs), fp); fclose(fp); + return(0); } -void readAssemblyFile() \ No newline at end of file +int readAssemblyFile() { + +} \ No newline at end of file From 6203e65bdccc6c5fb3d3406f0384280739ec29c9 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Fri, 31 May 2024 16:16:18 +0100 Subject: [PATCH 3/9] add assembly file reading --- src/fileaccess.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/fileaccess.c b/src/fileaccess.c index e3e5df8..3ee3831 100644 --- a/src/fileaccess.c +++ b/src/fileaccess.c @@ -1,32 +1,56 @@ #include #include -bool isValidFileFormat(char filename[], char expectedExtension[]){ +#define MAX_ASM_LINE_LENGTH 100 + +int isValidFileFormat(char filename[], char expectedExtension[]){ int *pointLoc = strrchr(filename, '.'); if(pointLoc != NULL){ if(strcmp(pointLoc, expectedExtension)==0){ - return true; + return(1); } } - return false; + return(0); } int writeBinaryFile(word instrs[], char outputFile[]){ if (!isValidFileFormat(filename, "bin")){ - return(1); + return(-1); } FILE *fp; fp = fopen(outputFile, "wb"); + + if(fp == NULL){ + return(-1); + } + fwrite(instrs, 4, sizeof(instrs), fp); fclose(fp); return(0); } -int readAssemblyFile() { +int readAssemblyFile(char inputFile[]) { + if (!isValidFileFormat(filename, "s")){ + return(1); + } + FILE *fp; + char savedLine[MAX_ASM_LINE_LENGTH]; + + fp = fopen(inputFile, "r"); + + if(fp == NULL){ + return(-1); + } + + while (fgets(savedLine, MAX_ASM_LINE_LENGTH-1, fp) != NULL) { + //pass line to parser + } + + return(0); } \ No newline at end of file From 1ff18a4fb9b2e9da595e34d02e9617a9492ab88e Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Mon, 3 Jun 2024 21:23:18 +0100 Subject: [PATCH 4/9] rewrite error exits for consistency --- src/assemblylineconversion.c | 3 +++ src/assemblylineconversion.h | 0 src/fileaccess.c | 18 +++++++++++------- 3 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 src/assemblylineconversion.c create mode 100644 src/assemblylineconversion.h diff --git a/src/assemblylineconversion.c b/src/assemblylineconversion.c new file mode 100644 index 0000000..f612329 --- /dev/null +++ b/src/assemblylineconversion.c @@ -0,0 +1,3 @@ +#include +#include + diff --git a/src/assemblylineconversion.h b/src/assemblylineconversion.h new file mode 100644 index 0000000..e69de29 diff --git a/src/fileaccess.c b/src/fileaccess.c index 3ee3831..621de3c 100644 --- a/src/fileaccess.c +++ b/src/fileaccess.c @@ -3,6 +3,7 @@ #define MAX_ASM_LINE_LENGTH 100 +//validates inputted charlist as valid filename against expected extension int isValidFileFormat(char filename[], char expectedExtension[]){ int *pointLoc = strrchr(filename, '.'); @@ -14,10 +15,11 @@ int isValidFileFormat(char filename[], char expectedExtension[]){ return(0); } +//writes a list of words (list of binary instructions) to a named output file int writeBinaryFile(word instrs[], char outputFile[]){ if (!isValidFileFormat(filename, "bin")){ - return(-1); + exit(EXIT_FAILURE); } FILE *fp; @@ -25,18 +27,20 @@ int writeBinaryFile(word instrs[], char outputFile[]){ fp = fopen(outputFile, "wb"); if(fp == NULL){ - return(-1); + exit(EXIT_FAILURE); } - fwrite(instrs, 4, sizeof(instrs), fp); + fwrite(instrs, sizeof(word), sizeof(instrs), fp); fclose(fp); - return(0); + exit(EXIT_SUCCESS); } + +//reads assembly file of "inputFile" name, int readAssemblyFile(char inputFile[]) { if (!isValidFileFormat(filename, "s")){ - return(1); + exit(EXIT_FAILURE); } FILE *fp; @@ -45,12 +49,12 @@ int readAssemblyFile(char inputFile[]) { fp = fopen(inputFile, "r"); if(fp == NULL){ - return(-1); + exit(EXIT_FAILURE); } while (fgets(savedLine, MAX_ASM_LINE_LENGTH-1, fp) != NULL) { //pass line to parser } - return(0); + exit(EXIT_SUCCESS); } \ No newline at end of file From 43dd6be707f8e1834a1cdabdf75d68f317e9cd1c Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Mon, 3 Jun 2024 21:46:22 +0100 Subject: [PATCH 5/9] add a64instruction structs from emulator --- src/a64instruction.h | 28 +++++++++++ src/a64instruction_Branch.h | 41 +++++++++++++++ src/a64instruction_DP.h | 7 +++ src/a64instruction_DPImmediate.h | 42 ++++++++++++++++ src/a64instruction_DPRegister.h | 58 ++++++++++++++++++++++ src/a64instruction_SingleTransfer.h | 47 ++++++++++++++++++ src/a64instruction_global.h | 14 ++++++ src/assemblylineconversion.c | 3 -- src/parser.c | 11 ++++ src/{assemblylineconversion.h => parser.h} | 0 10 files changed, 248 insertions(+), 3 deletions(-) create mode 100644 src/a64instruction.h create mode 100644 src/a64instruction_Branch.h create mode 100644 src/a64instruction_DP.h create mode 100644 src/a64instruction_DPImmediate.h create mode 100644 src/a64instruction_DPRegister.h create mode 100644 src/a64instruction_SingleTransfer.h create mode 100644 src/a64instruction_global.h delete mode 100644 src/assemblylineconversion.c create mode 100644 src/parser.c rename src/{assemblylineconversion.h => parser.h} (100%) diff --git a/src/a64instruction.h b/src/a64instruction.h new file mode 100644 index 0000000..463e75a --- /dev/null +++ b/src/a64instruction.h @@ -0,0 +1,28 @@ +#ifndef __A64INSTRUCTION__ +#define __A64INSTRUCTION__ +#include "a64instruction_DPImmediate.h" +#include "a64instruction_Branch.h" +#include "a64instruction_SingleTransfer.h" + +// Define the types of instructions in subset of the AArch64 Instruction Set implemented. +// Each type is defined by the format of the instruction's operand(s). +typedef enum { + a64inst_DPIMMEDIATE, + a64inst_DPREGISTER, + a64inst_SINGLETRANSFER, + a64inst_LOADLITERAL, + a64inst_BRANCH, + a64inst_HALT +} a64inst_type; + +// Structure the holds the type and operand data of an instruction +typedef struct { + a64inst_type type; + union { + a64inst_DPImmediateData DPImmediateData; + a64inst_BranchData BranchData; + a64inst_SingleTransferData SingleTransferData; + } data; +} a64inst_instruction; + +#endif diff --git a/src/a64instruction_Branch.h b/src/a64instruction_Branch.h new file mode 100644 index 0000000..1681768 --- /dev/null +++ b/src/a64instruction_Branch.h @@ -0,0 +1,41 @@ +#include +#include "a64instruction_global.h" +#include "global.h" + +typedef enum { + a64inst_UNCONDITIONAL = 0, + a64inst_REGISTER = 1, + a64inst_CONDITIONAL = 2 +} a64inst_BranchType; + +typedef struct { + word unconditionalOffset; +} a64inst_Branch_UnconditionalData; + +typedef struct { + a64inst_regSpecifier src; +} a64inst_Branch_RegisterData; + +typedef enum { + EQ = 0, // Equal + NE = 1, // Not Equal + GE = 10, // Signed greater or equal + LT = 11, // Signed less than + GT = 12, // Signed greater than + LE = 13, // signed less than or equal + AL = 14 // Always +} a64inst_ConditionType; //a64inst_Branch_ConditionType? + +typedef struct { + a64inst_ConditionType cond; + word offset; +} a64inst_Branch_ConditionalData; + +typedef struct { + a64inst_BranchType BranchType; + union { + a64inst_Branch_UnconditionalData unconditionalData; + a64inst_Branch_RegisterData registerData; + a64inst_Branch_ConditionalData conditionalData; + } processOpData; +} a64inst_BranchData; diff --git a/src/a64instruction_DP.h b/src/a64instruction_DP.h new file mode 100644 index 0000000..113f589 --- /dev/null +++ b/src/a64instruction_DP.h @@ -0,0 +1,7 @@ +// Denotes the type of arithmetic operations supported by the architecture +typedef enum { + a64inst_ADD = 0, + a64inst_ADDS = 1, + a64inst_SUB = 2, + a64inst_SUBS = 3 +} a64inst_arithmOp; diff --git a/src/a64instruction_DPImmediate.h b/src/a64instruction_DPImmediate.h new file mode 100644 index 0000000..8b5e68c --- /dev/null +++ b/src/a64instruction_DPImmediate.h @@ -0,0 +1,42 @@ +#include +#include "a64instruction_global.h" +#include "a64instruction_DP.h" + +// Denotes the type of data processing operation +typedef enum { + a64inst_DPI_ARITHM, + a64inst_DPI_WIDEMOV +} a64inst_DPIOpType; + +// Denotes the type of wide move operations supported by the architecture +typedef enum { + a64inst_MOVN = 0, + a64inst_UNDEFINED = 1, + a64inst_MOVZ = 2, + a64inst_MOVK = 3 +} a64inst_wideMovOp; + +// Holds data specific to arithmetic immediate data processing instructions +typedef struct { + bool shiftImmediate; + uint16_t immediate; + a64inst_regSpecifier src; +} a64inst_DPImmediate_ArithmData; + +// Holds data specific to wide move immediate data processing instructions +typedef struct { + uint8_t shiftScalar; + uint16_t immediate; +} a64inst_DPImmediate_WideMovData; + +// Holds data for immediate data processing instructions +typedef struct { + a64inst_regType regType; + a64inst_DPIOpType DPIOpType; + unsigned int processOp; + union { + a64inst_DPImmediate_ArithmData arithmData; + a64inst_DPImmediate_WideMovData wideMovData; + } processOpData; + a64inst_regSpecifier dest; +} a64inst_DPImmediateData; diff --git a/src/a64instruction_DPRegister.h b/src/a64instruction_DPRegister.h new file mode 100644 index 0000000..d0252ee --- /dev/null +++ b/src/a64instruction_DPRegister.h @@ -0,0 +1,58 @@ +#include +#include "a64instruction_global.h" +#include "a64instruction_DP.h" + +// Denotes the type of data processing operation +typedef enum { + a64inst_DPR_ARITHMLOGIC, + a64inst_DPR_MULTIPLY +} a64inst_DPROpType; + +// Denotes the logical operations supported by the architecture +typedef enum { + a64inst_AND = 0, + a64inst_OR = 1, + a64inst_XOR = 2, + a64inst_AND_FLAGGED = 3 +} a64inst_logicOp; + +// Denotes the different kinds of shifts supported by the architecture +typedef enum { + a64inst_LSL = 0, + a64inst_LSR = 1, + a64inst_ASR = 2, + a64inst_ROR = 3 +} a64inst_ShiftType; + +// Holds data specific to arithmetic/logic register data processing instructions +typedef struct { + enum { + a64inst_DPR_ARITHM = 0, + a64inst_DPR_LOGIC = 1 + } type; + a64inst_ShiftType shiftType; + bool negShiftedSrc2; // Guaranteed to be 0 for arithmetic instructions +} a64inst_DPRegister_ArithmLogicData; + +// Holds data specific to multiply register data processing instructions +typedef struct { + bool negProd; + a64inst_regSpecifier summand; +} a64inst_DPRegister_MultiplyData; + +// Holds data for register data processing instructions +typedef struct { + a64inst_regType regType; + a64inst_DPROpType DPROpType; + union { + a64inst_logicOp logicOp; + a64inst_arithmOp arithmOp; + } processOpId; + a64inst_regSpecifier src2; + union { + a64inst_DPRegister_ArithmLogicData arithmLogicData; + a64inst_DPRegister_MultiplyData multiplydata; + } processOpData; + a64inst_regSpecifier src1; + a64inst_regSpecifier dest; +} a64inst_DPRegisterData; diff --git a/src/a64instruction_SingleTransfer.h b/src/a64instruction_SingleTransfer.h new file mode 100644 index 0000000..f661116 --- /dev/null +++ b/src/a64instruction_SingleTransfer.h @@ -0,0 +1,47 @@ +#include +#include "a64instruction_global.h" +#include "global.h" + +typedef enum { + a64inst_SINGLE_TRANSFER_SINGLE_DATA_TRANSFER = 1, + a64inst_SINGLE_TRANSFER_LOAD_LITERAL = 0 +} a64inst_SingleTransferType; + +typedef enum { + a64inst_STORE, + a64inst_LOAD +} a64inst_TransferType; + +typedef enum { + a64inst_REGISTER_OFFSET = 2, + a64inst_PRE_INDEXED = 1, + a64inst_POST_INDEXED = 0, + a64inst_UNSIGNED_OFFSET = 3 +} a64inst_AddressingMode; + +typedef struct { + a64inst_TransferType transferType; + a64inst_AddressingMode addressingMode; + + union { + a64inst_regSpecifier offsetReg; + uint16_t indexedOffset; + uint16_t unsignedOffset; + } a64inst_addressingModeData; + + a64inst_regSpecifier base; +} a64inst_SingleDataTransferData; + +typedef struct { + uint32_t offset; +} a64inst_LoadLiteralData; + +typedef struct { + a64inst_SingleTransferType SingleTransferOpType; + a64inst_regType regType; + a64inst_regSpecifier target; + union { + a64inst_SingleDataTransferData singleDataTransferData; + a64inst_LoadLiteralData loadLiteralData; + } processOpData; +} a64inst_SingleTransferData; diff --git a/src/a64instruction_global.h b/src/a64instruction_global.h new file mode 100644 index 0000000..489fe06 --- /dev/null +++ b/src/a64instruction_global.h @@ -0,0 +1,14 @@ +#ifndef __A64INSTRUCTION_GLOBAL__ +#define __A64INSTRUCTION_GLOBAL__ +#include + +// Specifies the register being referred to +typedef uint8_t a64inst_regSpecifier; + +// Denotes the type of register being referred to +typedef enum { + a64inst_W = 0, + a64inst_R = 1 +} a64inst_regType; + +#endif diff --git a/src/assemblylineconversion.c b/src/assemblylineconversion.c deleted file mode 100644 index f612329..0000000 --- a/src/assemblylineconversion.c +++ /dev/null @@ -1,3 +0,0 @@ -#include -#include - diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..1572ef5 --- /dev/null +++ b/src/parser.c @@ -0,0 +1,11 @@ +#include +#include +#include + +#include + +//takes input string, read from asm file and returns +//input as an a64 instruction +a64inst_instruction parser(char asmLine[]){ + +} \ No newline at end of file diff --git a/src/assemblylineconversion.h b/src/parser.h similarity index 100% rename from src/assemblylineconversion.h rename to src/parser.h From ba1b614fc1d4ca18284be1bb401bc45a344f3072 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Mon, 3 Jun 2024 22:02:41 +0100 Subject: [PATCH 6/9] comment code for understanding --- src/fileaccess.c | 7 +++++-- src/parser.c | 14 +++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/fileaccess.c b/src/fileaccess.c index 621de3c..c425ee7 100644 --- a/src/fileaccess.c +++ b/src/fileaccess.c @@ -37,7 +37,8 @@ int writeBinaryFile(word instrs[], char outputFile[]){ } -//reads assembly file of "inputFile" name, +//reads assembly file of "inputFile" name, and passes +//each line into a parser int readAssemblyFile(char inputFile[]) { if (!isValidFileFormat(filename, "s")){ exit(EXIT_FAILURE); @@ -53,7 +54,9 @@ int readAssemblyFile(char inputFile[]) { } while (fgets(savedLine, MAX_ASM_LINE_LENGTH-1, fp) != NULL) { - //pass line to parser + // removes newline char before saving them + savedLine[strcspn(savedLine, "\n")] = 0; + } exit(EXIT_SUCCESS); diff --git a/src/parser.c b/src/parser.c index 1572ef5..1c56c8e 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,11 +1,19 @@ #include #include -#include +#include "parser.h" -#include +#include "a64instruction.h" //takes input string, read from asm file and returns //input as an a64 instruction + +//TODO: +// - use string matching to get opcode, and operands +// - check operand count +// - match opcode to a64 struct types +// - count operands and match type/values +// - generate final a64inst and return + a64inst_instruction parser(char asmLine[]){ - + } \ No newline at end of file From 036e163fe8a0ea92d5c0cc863ae08dfbb4320d3d Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Mon, 3 Jun 2024 23:07:31 +0100 Subject: [PATCH 7/9] classify asm line type, tokenise operands --- src/fileaccess.c | 4 +--- src/parser.c | 39 ++++++++++++++++++++++++++++++++++++++- src/parser.h | 1 + 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/fileaccess.c b/src/fileaccess.c index c425ee7..14bf70d 100644 --- a/src/fileaccess.c +++ b/src/fileaccess.c @@ -1,8 +1,6 @@ #include #include -#define MAX_ASM_LINE_LENGTH 100 - //validates inputted charlist as valid filename against expected extension int isValidFileFormat(char filename[], char expectedExtension[]){ int *pointLoc = strrchr(filename, '.'); @@ -45,7 +43,7 @@ int readAssemblyFile(char inputFile[]) { } FILE *fp; - char savedLine[MAX_ASM_LINE_LENGTH]; + char savedLine[sizeof(a64inst_instruction)]; fp = fopen(inputFile, "r"); diff --git a/src/parser.c b/src/parser.c index 1c56c8e..1d7c2cd 100644 --- a/src/parser.c +++ b/src/parser.c @@ -14,6 +14,43 @@ // - count operands and match type/values // - generate final a64inst and return -a64inst_instruction parser(char asmLine[]){ +char *splitOperands(char* str, int operandCount, char *operands[]){ + char *operandsDupe = strdup(str); + int operandCount = 0; + char *operand = strtok(operandsDupe, OPERAND_DELIMITER); + operands[0] = operand; + + while (operand != NULL){ + operandCount++; + operand = strtok(NULL, OPERAND_DELIMITER); + operands[operandCount] = operand; + } + return(operands); + +} + +a64inst_instruction *tokeniser(char asmLine[]){ + a64inst_instruction *instr = malloc(sizeof(a64inst_instruction)); + if (instr == NULL){ + exit(EXIT_FAILURE); + } + + //"opcode operand1, {operand2}, ..." + char *stringptr = strdup(asmLine); + + char *opcode = strtok(stringptr, " "); + char *operands = strtok(NULL, ""); + + if(opcode[0]=="."){ + //type is directive + } else if(opcode[strlen(opcode)-1]==":") { + //type is label + } else { + //type is instruction + int operandCount = 0; + const char *operandList[4]; + splitOperands(operands, &operandCount, operandList); + } + } \ No newline at end of file diff --git a/src/parser.h b/src/parser.h index e69de29..5542aca 100644 --- a/src/parser.h +++ b/src/parser.h @@ -0,0 +1 @@ +#define OPERAND_DELIMITER ", " \ No newline at end of file From cadac4e1bbcf3d0ec02629713bd16391a80243b3 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Mon, 3 Jun 2024 23:09:40 +0100 Subject: [PATCH 8/9] rename parser funcs for clarity --- src/parser.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/parser.c b/src/parser.c index 1d7c2cd..1c30fc3 100644 --- a/src/parser.c +++ b/src/parser.c @@ -8,13 +8,13 @@ //input as an a64 instruction //TODO: -// - use string matching to get opcode, and operands -// - check operand count +// - use string matching to get opcode, and operands (DONE) +// - check operand count (DONE) // - match opcode to a64 struct types // - count operands and match type/values // - generate final a64inst and return -char *splitOperands(char* str, int operandCount, char *operands[]){ +char *tokeniseOperands(char* str, int operandCount, char *operands[]){ char *operandsDupe = strdup(str); int operandCount = 0; char *operand = strtok(operandsDupe, OPERAND_DELIMITER); @@ -29,7 +29,7 @@ char *splitOperands(char* str, int operandCount, char *operands[]){ } -a64inst_instruction *tokeniser(char asmLine[]){ +a64inst_instruction *parser(char asmLine[]){ a64inst_instruction *instr = malloc(sizeof(a64inst_instruction)); if (instr == NULL){ exit(EXIT_FAILURE); @@ -49,8 +49,11 @@ a64inst_instruction *tokeniser(char asmLine[]){ //type is instruction int operandCount = 0; const char *operandList[4]; - splitOperands(operands, &operandCount, operandList); + tokeniseOperands(operands, &operandCount, operandList); } -} \ No newline at end of file + return(a64inst_instruction); + +} + From 0f04ac9e2271e3983d50573172ae2c1cd5a78475 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Tue, 4 Jun 2024 14:53:30 +0100 Subject: [PATCH 9/9] rename fileaccess --- src/{fileaccess.c => fileio.c} | 1 + src/parser.c | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) rename src/{fileaccess.c => fileio.c} (96%) diff --git a/src/fileaccess.c b/src/fileio.c similarity index 96% rename from src/fileaccess.c rename to src/fileio.c index 14bf70d..45b25e8 100644 --- a/src/fileaccess.c +++ b/src/fileio.c @@ -37,6 +37,7 @@ int writeBinaryFile(word instrs[], char outputFile[]){ //reads assembly file of "inputFile" name, and passes //each line into a parser +//TODO: allocate whole file in memory, line-by-line int readAssemblyFile(char inputFile[]) { if (!isValidFileFormat(filename, "s")){ exit(EXIT_FAILURE); diff --git a/src/parser.c b/src/parser.c index 1c30fc3..9f5f526 100644 --- a/src/parser.c +++ b/src/parser.c @@ -14,7 +14,10 @@ // - count operands and match type/values // - generate final a64inst and return -char *tokeniseOperands(char* str, int operandCount, char *operands[]){ +//takes string of operands, and reference to operandcounter +//takes input of result array +//outputs array of operands +void tokeniseOperands(char* str, int operandCount, char *operands[]){ char *operandsDupe = strdup(str); int operandCount = 0; char *operand = strtok(operandsDupe, OPERAND_DELIMITER); @@ -25,10 +28,10 @@ char *tokeniseOperands(char* str, int operandCount, char *operands[]){ operand = strtok(NULL, OPERAND_DELIMITER); operands[operandCount] = operand; } - return(operands); - } +//takes inputted assembly line and returns a +//pointer to an abstract representation of the instruction a64inst_instruction *parser(char asmLine[]){ a64inst_instruction *instr = malloc(sizeof(a64inst_instruction)); if (instr == NULL){ @@ -36,6 +39,7 @@ a64inst_instruction *parser(char asmLine[]){ } //"opcode operand1, {operand2}, ..." + //duplicated as strtok modifies the input string char *stringptr = strdup(asmLine); char *opcode = strtok(stringptr, " ");