From 04dda33987e478800ee7feee263221ab58b97a6f Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Sun, 9 Jun 2024 22:21:30 +0100 Subject: [PATCH 1/9] calculate base register from input --- src/parser.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/parser.c b/src/parser.c index 1bcf31e..192278e 100644 --- a/src/parser.c +++ b/src/parser.c @@ -18,14 +18,21 @@ //calculate offsets from string void calcluateAddressFormat(a64inst_instruction *instr, char *operandList[]){ + char *baseRegister = operandList[1]; + baseRegister++; + baseRegister++; + char *endptr; + uint8_t base = strtol(baseRegister, endptr, 10); + instr->data.SingleTransferData.processOpData.singleDataTransferData.base = base; if(strcmp(operandList[2][strlen(operandList[1])-1], "!")==0){ instr->data.SingleTransferData.processOpData.singleDataTransferData.addressingMode = a64inst_PRE_INDEXED; + } else if(strcmp(operandList[1][strlen(operandList[0])-1], "]") == 0) { //post-indexed instr->data.SingleTransferData.processOpData.singleDataTransferData.addressingMode = a64inst_POST_INDEXED; - } else if( (strcmp(operandList[2][strlen(operandList[1])-1], "x") == 0) - || (strcmp(operandList[2][strlen(operandList[1])-1], "w") == 0)){ + } else if( (strcmp(operandList[2][0], "x") == 0) + || (strcmp(operandList[2][0], "w") == 0)){ //register instr->data.SingleTransferData.processOpData.singleDataTransferData.processOpData.addressingMode = a64inst_REGISTER_OFFSET; } else { @@ -43,7 +50,7 @@ void generateLoadStoreOperands(a64inst_instruction *instr, char *opcode, char *o instr->data.SingleTransferData.regType = 0; } char *endptr; - instr->data.SingleTransferData.target = strtol(operandList[0][0]+1, endptr, 2); + instr->data.SingleTransferData.target = strtol(operandList[0][0]+1, endptr, 10); calcluateAddressFormat(instr, operandList); break; case a64inst_LOADLITERAL: @@ -60,7 +67,7 @@ void generateBranchOperands(a64inst_instruction *instr, char* opcode, char *oper break; case a64inst_REGISTER: char *endptr; - instr->data.BranchData.processOpData.registerData.src = strtol(operandList[0] + 1, endptr, 2) + instr->data.BranchData.processOpData.registerData.src = strtol(operandList[0] + 1, endptr, 10) break; case a64inst_CONDITIONAL: char* condition = strtok(strdup(opcode), "b."); From 4098ea5a5f26c8d243195219df43d376fb6a3fdb Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Sun, 9 Jun 2024 22:43:37 +0100 Subject: [PATCH 2/9] calculate offsets for different store instructions --- src/parser.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/parser.c b/src/parser.c index 192278e..699bdb6 100644 --- a/src/parser.c +++ b/src/parser.c @@ -17,8 +17,8 @@ // - CREATE FUNC TO TIDY UP OPERANDS IN DP //calculate offsets from string -void calcluateAddressFormat(a64inst_instruction *instr, char *operandList[]){ - char *baseRegister = operandList[1]; +void calcluateAddressFormat(a64inst_instruction *instr, char *operandList[], int numOperands){ + char *baseRegister = strdup(operandList[1]); baseRegister++; baseRegister++; char *endptr; @@ -27,20 +27,39 @@ void calcluateAddressFormat(a64inst_instruction *instr, char *operandList[]){ if(strcmp(operandList[2][strlen(operandList[1])-1], "!")==0){ instr->data.SingleTransferData.processOpData.singleDataTransferData.addressingMode = a64inst_PRE_INDEXED; - + char *offsetParam = strdup(operandList[2]); + offsetParam++; + instr->data.SingleTransferData.processOpData.singleDataTransferData.a64inst_addressingModeData.indexedOffset = strtol(operandList[2], endptr, 10); } else if(strcmp(operandList[1][strlen(operandList[0])-1], "]") == 0) { //post-indexed instr->data.SingleTransferData.processOpData.singleDataTransferData.addressingMode = a64inst_POST_INDEXED; + char *offsetParam = strdup(operandList[2]); + offsetParam++; + instr->data.SingleTransferData.processOpData.singleDataTransferData.a64inst_addressingModeData.indexedOffset = strtol(operandList[2], endptr, 10); } else if( (strcmp(operandList[2][0], "x") == 0) || (strcmp(operandList[2][0], "w") == 0)){ //register - instr->data.SingleTransferData.processOpData.singleDataTransferData.processOpData.addressingMode = a64inst_REGISTER_OFFSET; + instr->data.SingleTransferData.processOpData.singleDataTransferData.addressingMode = a64inst_REGISTER_OFFSET; + char *offsetRegister = strdup(operandList[2]); + offsetRegister++; + instr->data.SingleTransferData.processOpData.singleDataTransferData.addressingModeData.offsetReg = strtol(offsetRegister, endptr, 10); } else { instr->data.SingleTransferData.processOpData.singleDataTransferData.addressingMode = a64inst_UNSIGNED_OFFSET; + if(numOperands==3){ + char *offsetParam = strdup(operandList[2]); + offsetParam++; + int offset = strtol(operandList[2], endptr, 10); + if(instr->data.SingleTransferData.regType == 1){ + instr->data.SingleTransferData.processOpData.singleDataTransferData.a64inst_addressingModeData.unsignedOffset = offset/8; + } else { + instr->data.SingleTransferData.processOpData.singleDataTransferData.a64inst_addressingModeData.unsignedOffset = offset/4; + + } + } } } -void generateLoadStoreOperands(a64inst_instruction *instr, char *opcode, char *operandList[]){ +void generateLoadStoreOperands(a64inst_instruction *instr, char *opcode, char *operandList[], int numOperands){ switch(instr->type){ case a64inst_SINGLETRANSFER: if(strcmp(operandList[0][0], "x")==0){ @@ -51,7 +70,7 @@ void generateLoadStoreOperands(a64inst_instruction *instr, char *opcode, char *o } char *endptr; instr->data.SingleTransferData.target = strtol(operandList[0][0]+1, endptr, 10); - calcluateAddressFormat(instr, operandList); + calcluateAddressFormat(instr, operandList, numOperands); break; case a64inst_LOADLITERAL: break; @@ -102,7 +121,7 @@ int classifyDPInst(char *operandList[]){ isOperandRegister(operandList[2])); } -void classifyOpcode(char* opcode, a64inst_instruction *instr, char *operandList[]){ +void classifyOpcode(char* opcode, a64inst_instruction *instr, char *operandList[], int numOperands){ int isUnconditional = strcmp(opcode, "b"); int isRegister = strcmp(opcode, "br"); int isLoad = strcmp(opcode, "ldr"); @@ -149,7 +168,7 @@ void classifyOpcode(char* opcode, a64inst_instruction *instr, char *operandList[ } } -char *tokeniseOperands(char* str, int operandCount, char *operands[]){ +char *tokeniseOperands(char* str, int operandCount, char *operands[], int numOperands){ char *operandsDupe = strdup(str); char *operand = strtok(operandsDupe, OPERAND_DELIMITER); operands[0] = operand; @@ -159,11 +178,13 @@ char *tokeniseOperands(char* str, int operandCount, char *operands[]){ operand = strtok(NULL, OPERAND_DELIMITER); operands[operandCount] = operand; } + numOperands = operandCount+1; } //takes inputted assembly line and returns a //pointer to an abstract representation of the instruction a64inst_instruction *parser(char asmLine[]){ + int numOperands = 0; a64inst_instruction *instr = malloc(sizeof(a64inst_instruction)); if (instr == NULL){ exit(EXIT_FAILURE); @@ -196,7 +217,7 @@ a64inst_instruction *parser(char asmLine[]){ //type is instruction int operandCount = 0; const char *operandList[4]; - tokeniseOperands(operands, &operandCount, operandList); + tokeniseOperands(operands, &operandCount, operandList, &numOperands); } From 44bb327b7d13a85d5095c0265cb9dd99fda9670d Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Sun, 9 Jun 2024 22:54:27 +0100 Subject: [PATCH 3/9] begin formulating parser pipeline --- src/parser.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/parser.c b/src/parser.c index 699bdb6..40e264f 100644 --- a/src/parser.c +++ b/src/parser.c @@ -36,8 +36,8 @@ void calcluateAddressFormat(a64inst_instruction *instr, char *operandList[], in char *offsetParam = strdup(operandList[2]); offsetParam++; instr->data.SingleTransferData.processOpData.singleDataTransferData.a64inst_addressingModeData.indexedOffset = strtol(operandList[2], endptr, 10); - } else if( (strcmp(operandList[2][0], "x") == 0) - || (strcmp(operandList[2][0], "w") == 0)){ + } else if( (isOperandRegister(operandList[2][0], "x") == 1) + || (isOperandRegister(operandList[2][0], "w") == 1)){ //register instr->data.SingleTransferData.processOpData.singleDataTransferData.addressingMode = a64inst_REGISTER_OFFSET; char *offsetRegister = strdup(operandList[2]); @@ -53,7 +53,6 @@ void calcluateAddressFormat(a64inst_instruction *instr, char *operandList[], in instr->data.SingleTransferData.processOpData.singleDataTransferData.a64inst_addressingModeData.unsignedOffset = offset/8; } else { instr->data.SingleTransferData.processOpData.singleDataTransferData.a64inst_addressingModeData.unsignedOffset = offset/4; - } } } @@ -218,6 +217,27 @@ a64inst_instruction *parser(char asmLine[]){ int operandCount = 0; const char *operandList[4]; tokeniseOperands(operands, &operandCount, operandList, &numOperands); + classifyOpcode(opcode, instr, operandList, operandCount); + switch(instr->type){ + case a64inst_BRANCH: + generateBranchOperands(instr, opcode, operandList); + break; + case a64inst_SINGLETRANSFER: + generateLoadStoreOperands(instr, opcode, operandList, numOperands); + break; + case a64inst_LOADLITERAL: + generateLoadStoreOperands(instr, opcode, operandList, numOperands); + break; + case a64inst_DPREGISTER: + //generate DP operands; + break; + case a64inst_DPIMMEDIATE: + //generate DP operands; + break; + default: + printf("INVALID INSTRUCTION"); + break; + } } From d0be871e8f5bc3699a9710ea4ffaada01fc489c0 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Sun, 9 Jun 2024 23:07:45 +0100 Subject: [PATCH 4/9] generate offset operand for load literal with immediate value --- src/parser.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/parser.c b/src/parser.c index 40e264f..9a1ac25 100644 --- a/src/parser.c +++ b/src/parser.c @@ -153,11 +153,19 @@ void classifyOpcode(char* opcode, a64inst_instruction *instr, char *operandList[ } } else { instr->type = a64inst_LOADLITERAL; - //instr->data.processOpData.offset = {} to be defined by symbol table + if(strcmp(operandList[0][0], "#")==0){ + //offset is immediate + char *immOffset = strdup(operandList[0]) + immOffset++; + char *endptr; + int offset = strtol(immOffset, endptr, 10); + instr->data.SingleTransferData.processOpData.loadLiteralData.offset = offset; + } else { + //offset is literal, use symbol table and calculate difference + } } } else { - int numOperands = sizeof(operandList) / sizeof(operandList[0]) if(classifyDPInst(operandList)){ instr->type = a64inst_DPREGISTER; } else { From 92719b6b33159a328624e95d341bc83ada815719 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Sun, 9 Jun 2024 23:10:39 +0100 Subject: [PATCH 5/9] comments for clarity in parser --- src/parser.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/parser.c b/src/parser.c index 9a1ac25..c0a3caa 100644 --- a/src/parser.c +++ b/src/parser.c @@ -136,7 +136,6 @@ void classifyOpcode(char* opcode, a64inst_instruction *instr, char *operandList[ instr->data.BranchData.BranchType = a64inst_REGISTER; } else { instr->data.BranchData.BranchType = a64inst_CONDITIONAL; - //instr->data.branchData.processOpData.cond = {remove first two chars of opcode} } generateBranchOperands(instr, opcode, operandList); } else if(isLoad == 0 || isStore == 0){ @@ -224,8 +223,11 @@ a64inst_instruction *parser(char asmLine[]){ //type is instruction int operandCount = 0; const char *operandList[4]; + //generate list of operands tokeniseOperands(operands, &operandCount, operandList, &numOperands); + //categorise instruction type from opcode and operands classifyOpcode(opcode, instr, operandList, operandCount); + //define struct values according to operands and type switch(instr->type){ case a64inst_BRANCH: generateBranchOperands(instr, opcode, operandList); From 6153db77377dc812983096fff61e9337f49fa88d Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Tue, 11 Jun 2024 17:35:23 +0100 Subject: [PATCH 6/9] fix compile issues git add . :) --- src/assemble.c | 1 + src/emulate.c | 6 +++ src/parser.c | 130 ++++++++++++++++++++++++------------------------- src/parser.h | 3 -- 4 files changed, 72 insertions(+), 68 deletions(-) mode change 100755 => 100644 src/emulate.c diff --git a/src/assemble.c b/src/assemble.c index ae760c0..aa54b4e 100755 --- a/src/assemble.c +++ b/src/assemble.c @@ -1,5 +1,6 @@ #include #include +#include "parser.c" int main(int argc, char **argv) { diff --git a/src/emulate.c b/src/emulate.c old mode 100755 new mode 100644 index 82245e9..be41f56 --- a/src/emulate.c +++ b/src/emulate.c @@ -8,6 +8,11 @@ #include "decode.h" #include "execute.h" +int main(int arg, char **argv){ + return EXIT_SUCCESS; +} + +/* extern a64inst_instruction *decode(word w); int main(int argc, char **argv) { @@ -59,3 +64,4 @@ int main(int argc, char **argv) { return EXIT_SUCCESS; } +*/ diff --git a/src/parser.c b/src/parser.c index c0a3caa..fcef57b 100644 --- a/src/parser.c +++ b/src/parser.c @@ -12,43 +12,37 @@ // - use string matching to get opcode, and operands (DONE) // - check operand count (DONE) // - match opcode to a64 struct types (DONE) -// - count operands and match type/values -// - generate final a64inst and return +// - count operands and match type/values (DONE) +// - generate final a64inst and return (TODO: DP instrs) +// - ASK ABOUT OFFSET CALCULATION // - CREATE FUNC TO TIDY UP OPERANDS IN DP +int isOperandRegister(char *operand){ + return((strcmp(&(operand[0]), "x")==0) || (strcmp(&(operand[0]), "w")==0)); +} + //calculate offsets from string void calcluateAddressFormat(a64inst_instruction *instr, char *operandList[], int numOperands){ - char *baseRegister = strdup(operandList[1]); - baseRegister++; - baseRegister++; char *endptr; - uint8_t base = strtol(baseRegister, endptr, 10); + uint8_t base = strtol(&(operandList[1][2]), &endptr, 10); instr->data.SingleTransferData.processOpData.singleDataTransferData.base = base; - if(strcmp(operandList[2][strlen(operandList[1])-1], "!")==0){ + if(strcmp(&(operandList[2][strlen(operandList[1])-1]), "!")==0){ instr->data.SingleTransferData.processOpData.singleDataTransferData.addressingMode = a64inst_PRE_INDEXED; - char *offsetParam = strdup(operandList[2]); - offsetParam++; - instr->data.SingleTransferData.processOpData.singleDataTransferData.a64inst_addressingModeData.indexedOffset = strtol(operandList[2], endptr, 10); - } else if(strcmp(operandList[1][strlen(operandList[0])-1], "]") == 0) { + instr->data.SingleTransferData.processOpData.singleDataTransferData.a64inst_addressingModeData.indexedOffset = strtol(&(operandList[2][1]), &endptr, 10); + } else if(strcmp(&(operandList[1][strlen(operandList[0])-1]), "]") == 0) { //post-indexed instr->data.SingleTransferData.processOpData.singleDataTransferData.addressingMode = a64inst_POST_INDEXED; - char *offsetParam = strdup(operandList[2]); - offsetParam++; - instr->data.SingleTransferData.processOpData.singleDataTransferData.a64inst_addressingModeData.indexedOffset = strtol(operandList[2], endptr, 10); - } else if( (isOperandRegister(operandList[2][0], "x") == 1) - || (isOperandRegister(operandList[2][0], "w") == 1)){ + instr->data.SingleTransferData.processOpData.singleDataTransferData.a64inst_addressingModeData.indexedOffset = strtol(&(operandList[2][1]), &endptr, 10); + } else if( (isOperandRegister(&(operandList[2][0])) == 1) + || (isOperandRegister(&(operandList[2][0])) == 1)){ //register instr->data.SingleTransferData.processOpData.singleDataTransferData.addressingMode = a64inst_REGISTER_OFFSET; - char *offsetRegister = strdup(operandList[2]); - offsetRegister++; - instr->data.SingleTransferData.processOpData.singleDataTransferData.addressingModeData.offsetReg = strtol(offsetRegister, endptr, 10); + instr->data.SingleTransferData.processOpData.singleDataTransferData.a64inst_addressingModeData.offsetReg = strtol(&(operandList[2][1]), &endptr, 10); } else { instr->data.SingleTransferData.processOpData.singleDataTransferData.addressingMode = a64inst_UNSIGNED_OFFSET; if(numOperands==3){ - char *offsetParam = strdup(operandList[2]); - offsetParam++; - int offset = strtol(operandList[2], endptr, 10); + int offset = strtol(&(operandList[2][1]), &endptr, 10); if(instr->data.SingleTransferData.regType == 1){ instr->data.SingleTransferData.processOpData.singleDataTransferData.a64inst_addressingModeData.unsignedOffset = offset/8; } else { @@ -61,59 +55,61 @@ void calcluateAddressFormat(a64inst_instruction *instr, char *operandList[], in void generateLoadStoreOperands(a64inst_instruction *instr, char *opcode, char *operandList[], int numOperands){ switch(instr->type){ case a64inst_SINGLETRANSFER: - if(strcmp(operandList[0][0], "x")==0){ + if(strcmp(&(operandList[0][0]), "x")==0){ //x-register instr->data.SingleTransferData.regType = 1; } else { instr->data.SingleTransferData.regType = 0; } char *endptr; - instr->data.SingleTransferData.target = strtol(operandList[0][0]+1, endptr, 10); + instr->data.SingleTransferData.target = strtol(&(operandList[0][0])+1, &endptr, 10); calcluateAddressFormat(instr, operandList, numOperands); break; case a64inst_LOADLITERAL: break; + default: + break; } } void generateBranchOperands(a64inst_instruction *instr, char* opcode, char *operandList[]){ + char *endptr; switch(instr->data.BranchData.BranchType){ case a64inst_UNCONDITIONAL: //define and sign extend immediate offset //use symbol table + printf("unconditional"); break; case a64inst_REGISTER: - char *endptr; - instr->data.BranchData.processOpData.registerData.src = strtol(operandList[0] + 1, endptr, 10) + instr->data.BranchData.processOpData.registerData.src = strtol(operandList[0] + 1, &endptr, 10); break; case a64inst_CONDITIONAL: - char* condition = strtok(strdup(opcode), "b."); - condition = strtok(NULL, ""); - if(strcmp(condition, "eq")==0){ - instr->data.branchData.processOpData.conditionalData.cond = EQ; - } else if (strcmp(condition, "ne")==0){ - instr->data.branchData.processOpData.conditionalData.cond = NE; - } else if (strcmp(condition, "ge")==0){ - instr->data.branchData.processOpData.conditionalData.cond = GE; - } else if (strcmp(condition, "lt")==0){ - instr->data.branchData.processOpData.conditionalData.cond = LT; - } else if (strcmp(condition, "gt")==0){ - instr->data.branchData.processOpData.conditionalData.cond = GT; - } else if (strcmp(condition, "le")==0){ - instr->data.branchData.processOpData.conditionalData.cond = LE; - } else if (srtcmp(condition, "al")==0){ - instr->data.branchData.processOpData.conditionalData.cond = AL; + { + char *condition = NULL; + condition = strcpy(condition, opcode); + condition += 2; + if(strcmp(condition, "eq")==0){ + instr->data.BranchData.processOpData.conditionalData.cond = EQ; + } else if (strcmp(condition, "ne")==0){ + instr->data.BranchData.processOpData.conditionalData.cond = NE; + } else if (strcmp(condition, "ge")==0){ + instr->data.BranchData.processOpData.conditionalData.cond = GE; + } else if (strcmp(condition, "lt")==0){ + instr->data.BranchData.processOpData.conditionalData.cond = LT; + } else if (strcmp(condition, "gt")==0){ + instr->data.BranchData.processOpData.conditionalData.cond = GT; + } else if (strcmp(condition, "le")==0){ + instr->data.BranchData.processOpData.conditionalData.cond = LE; + } else if (strcmp(condition, "al")==0){ + instr->data.BranchData.processOpData.conditionalData.cond = AL; + } + break; + //calculate offset from symbol table. } - break; - //calculate offset from symbol table. } } -int isOperandRegister(char *operand){ - return((strcmp(operand[0], "x")==0) || (strcmp(operand[0], "w")==0)); -} - int classifyDPInst(char *operandList[]){ return(isOperandRegister(operandList[0]) && isOperandRegister(operandList[1]) && @@ -144,20 +140,21 @@ void classifyOpcode(char* opcode, a64inst_instruction *instr, char *operandList[ if( *address == '['){ //type is register instr->type = a64inst_SINGLETRANSFER; - instr->data.singleTransferData.SingleTransferOpType = a64inst_SINGLE_TRANSFER_SINGLE_DATA_TRANSFER; + instr->data.SingleTransferData.SingleTransferOpType = a64inst_SINGLE_TRANSFER_SINGLE_DATA_TRANSFER; if(isLoad == 0){ - instr->data.SingleTransferData.transferType = a64inst_LOAD; + instr->data.SingleTransferData.processOpData.singleDataTransferData.transferType = a64inst_LOAD; } else { - instr->data.SingleTransferData.processOpData.transferType = a64inst_STORE; + instr->data.SingleTransferData.processOpData.singleDataTransferData.transferType = a64inst_STORE; } } else { instr->type = a64inst_LOADLITERAL; - if(strcmp(operandList[0][0], "#")==0){ + if(operandList[0][0] =='#'){ //offset is immediate - char *immOffset = strdup(operandList[0]) + char *immOffset = NULL; + immOffset = strcpy(immOffset, operandList[0]); immOffset++; - char *endptr; - int offset = strtol(immOffset, endptr, 10); + char *endptr = NULL; + int offset = strtol(immOffset, &endptr, 10); instr->data.SingleTransferData.processOpData.loadLiteralData.offset = offset; } else { //offset is literal, use symbol table and calculate difference @@ -174,17 +171,18 @@ void classifyOpcode(char* opcode, a64inst_instruction *instr, char *operandList[ } } -char *tokeniseOperands(char* str, int operandCount, char *operands[], int numOperands){ - char *operandsDupe = strdup(str); +void tokeniseOperands(char* str, int *operandCount, char *operands[], int *numOperands){ + char *operandsDupe = NULL; + operandsDupe = strcpy(operandsDupe, str); char *operand = strtok(operandsDupe, OPERAND_DELIMITER); operands[0] = operand; while (operand != NULL){ - operandCount++; + *operandCount = *(operandCount)+1; operand = strtok(NULL, OPERAND_DELIMITER); - operands[operandCount] = operand; + operands[*(operandCount)] = operand; } - numOperands = operandCount+1; + *(numOperands) = *(operandCount)+1; } //takes inputted assembly line and returns a @@ -203,7 +201,8 @@ a64inst_instruction *parser(char asmLine[]){ //"opcode operand1, {operand2}, ..." //duplicated as strtok modifies the input string - char *stringptr = strdup(asmLine); + char *stringptr = NULL; + stringptr = strcpy(stringptr, asmLine); char *opcode = strtok(stringptr, " "); char *operands = strtok(NULL, ""); @@ -212,17 +211,18 @@ a64inst_instruction *parser(char asmLine[]){ //type is directive instr->type = a64inst_DIRECTIVE; - } else if(strcmp(opcode[strlen(opcode)-1], ":") == 0) { + } else if(opcode[strlen(opcode)-1]== ':') { //type is label //add to symbol table instr->type = a64inst_LABEL; - char *opcodeCpy = strdup(opcode); + char *opcodeCpy = NULL; + opcodeCpy = strcpy(opcodeCpy, opcode); char *labelData = strtok(opcodeCpy, ":"); - instr->data.labelData.label = labelData; + instr->data.LabelData.label = labelData; } else { //type is instruction int operandCount = 0; - const char *operandList[4]; + char *operandList[4]; //generate list of operands tokeniseOperands(operands, &operandCount, operandList, &numOperands); //categorise instruction type from opcode and operands diff --git a/src/parser.h b/src/parser.h index 5c3a461..e303b58 100644 --- a/src/parser.h +++ b/src/parser.h @@ -1,5 +1,2 @@ -#ifndef __PARSERCONSTS__ -#define __PARSERCONSTS__ #define OPERAND_DELIMITER ", " #define HALT_ASM_CMD "and x0, x0, x0" -#endif \ No newline at end of file From 173bdf08ec6a6e59d0e63f18c29fb3799f1c3831 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Tue, 11 Jun 2024 20:23:00 +0100 Subject: [PATCH 7/9] fix incorrect fileio.c --- src/fileio.c | 88 ++++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/src/fileio.c b/src/fileio.c index 1dcdd77..96e5cd3 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -1,48 +1,56 @@ -#include #include #include -#include "fileio.h" -#include "global.h" -/* Loads a binary file located at filePath to memory, taking up a block of exactly memorySize bytes, - and returns the starting address of the data. If memorySize is insufficient to store the entire file, - an appropriate error is reported. Excess memory is set to 0 bit values. */ +#define MAX_ASM_LINE_LENGTH 100 -byte *fileio_loadBin(const char *filePath, size_t memorySize) { - FILE *file = fopen(filePath, "rb"); - if (file == NULL) { - fprintf(stderr, "Couldn't open %s!\n", filePath); - exit(EXIT_FAILURE); - } +int isValidFileFormat(char filename[], char expectedExtension[]){ + int *pointLoc = strrchr(filename, '.'); - byte *fileData = malloc(memorySize); - if (fileData == NULL) { - fprintf(stderr, "Ran out of memory attempting to load %s!\n", filePath); - exit(EXIT_FAILURE); - } - - // Loop while reading from the file yields data. Only terminates if EOF is reached or ERROR occurs. - // Explicitly deal with attempting to write too much data to memory block, rather than allow segfault. - const size_t byteCount = memorySize/sizeof(byte); - int i = 0; - while (fread(fileData + i, sizeof(byte), 1, file)) { - if (i >= byteCount) { - fprintf(stderr, "Attempting to load binary %s to memory of smaller size %zu!\n", filePath, memorySize); - exit(EXIT_FAILURE); + if(pointLoc != NULL){ + if(strcmp(pointLoc, expectedExtension)==0){ + return(1); } - - i++; } - - if (ferror(file)) { - fprintf(stderr, "Encountered error attempting to read %s!\n", filePath); - exit(EXIT_FAILURE); - } - assert(fclose(file) != EOF); - - // If part of memory block was left uninitialized, initialize it to zero. - if (i < byteCount) { - memset(fileData + i, 0, (byteCount - i) * sizeof(byte)); - } - return fileData; + return(0); +} + +int writeBinaryFile(word instrs[], char outputFile[]){ + + if (!isValidFileFormat(filename, "bin")){ + 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(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); } From 647f47e39d4ecf6019bd992a37e8c286048ba9e4 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Tue, 11 Jun 2024 21:16:47 +0100 Subject: [PATCH 8/9] rewrite fileio to load file into memory --- src/fileio.c | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/src/fileio.c b/src/fileio.c index 96e5cd3..32cfdb2 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -1,7 +1,7 @@ #include #include -#define MAX_ASM_LINE_LENGTH 100 +#define MAX_ASM_LINE_LENGTH 30 int isValidFileFormat(char filename[], char expectedExtension[]){ int *pointLoc = strrchr(filename, '.'); @@ -34,23 +34,47 @@ int writeBinaryFile(word instrs[], char outputFile[]){ return(0); } -int readAssemblyFile(char inputFile[]) { +char **readAssemblyFile(char inputFile[]) { if (!isValidFileFormat(filename, "s")){ - return(1); + return(NULL); } - FILE *fp; - char savedLine[MAX_ASM_LINE_LENGTH]; + FILE *fp = fopen(inputFile, "r"); - fp = fopen(inputFile, "r"); + if (fp == NULL){ + return(NULL); + } - if(fp == NULL){ - return(-1); + int lineCount = 0; + char ch; + while ((ch = fgetc(fp)) != EOF) + { + if (ch == '\n' || ch == '\0') + { + count++; + } } - while (fgets(savedLine, MAX_ASM_LINE_LENGTH-1, fp) != NULL) { - //pass line to parser + char **heap = malloc(sizeof(char *) * count); + + rewind(fp); + + for( int i=0; i Date: Tue, 11 Jun 2024 21:23:26 +0100 Subject: [PATCH 9/9] fix syntax errors in fileio.c --- src/assemble.c | 2 +- src/fileio.c | 20 ++++++++++---------- src/twopassassembly.c | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/assemble.c b/src/assemble.c index aa54b4e..4ed2733 100755 --- a/src/assemble.c +++ b/src/assemble.c @@ -1,8 +1,8 @@ #include #include #include "parser.c" +#include "fileio.c" int main(int argc, char **argv) { - return EXIT_SUCCESS; } diff --git a/src/fileio.c b/src/fileio.c index 32cfdb2..85fd8d1 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -4,7 +4,7 @@ #define MAX_ASM_LINE_LENGTH 30 int isValidFileFormat(char filename[], char expectedExtension[]){ - int *pointLoc = strrchr(filename, '.'); + char *pointLoc = strrchr(filename, '.'); if(pointLoc != NULL){ if(strcmp(pointLoc, expectedExtension)==0){ @@ -14,9 +14,9 @@ int isValidFileFormat(char filename[], char expectedExtension[]){ return(0); } -int writeBinaryFile(word instrs[], char outputFile[]){ +int writeBinaryFile(word instrs[], char outputFile[], int numInstrs){ - if (!isValidFileFormat(filename, "bin")){ + if (!isValidFileFormat(outputFile, "bin")){ return(-1); } @@ -28,14 +28,14 @@ int writeBinaryFile(word instrs[], char outputFile[]){ return(-1); } - fwrite(instrs, 4, sizeof(instrs), fp); + fwrite(instrs, 4, sizeof(word) * numInstrs, fp); fclose(fp); return(0); } char **readAssemblyFile(char inputFile[]) { - if (!isValidFileFormat(filename, "s")){ + if (!isValidFileFormat(inputFile, "s")){ return(NULL); } @@ -51,24 +51,24 @@ char **readAssemblyFile(char inputFile[]) { { if (ch == '\n' || ch == '\0') { - count++; + lineCount++; } } - char **heap = malloc(sizeof(char *) * count); + char **heap = malloc(sizeof(char *) * lineCount); rewind(fp); - for( int i=0; i