From 9f92eb476672ffe3ab2b1edc178ef9095814a597 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Wed, 12 Jun 2024 16:54:38 +0100 Subject: [PATCH 1/3] fix tokeniseOperands param order --- src/parser.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser.c b/src/parser.c index ba29ccf..64c748f 100644 --- a/src/parser.c +++ b/src/parser.c @@ -213,7 +213,7 @@ void parser_instruction(char asmLine[], a64inst_instruction *instr) { strcpy(stringptr, asmLine); char *opcode = strtok(stringptr, " "); - char *operands = strtok(NULL, ""); + char *operands = strtok(stringptr, ""); if(strcmp(opcode, ".int") == 0){ //type is directive From 53f5b05210c8e8e349c6a9f56355c0772487e549 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Wed, 12 Jun 2024 17:02:52 +0100 Subject: [PATCH 2/3] rewrite uses of strcpy w/ S --- src/parser.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/parser.c b/src/parser.c index 64c748f..4010764 100644 --- a/src/parser.c +++ b/src/parser.c @@ -21,9 +21,8 @@ //takes inputted char array and returns the integer of the operand, skipping the first character //e.g. for a passed "R32", it skips the 'R' and returns 32 int getOperandNumber(char *operand){ - char *operandCpy = NULL; - strcpy(operandCpy, operand); - operandCpy++; + char operandCpy[strlen(operand)]; + strcpy(operandCpy, operand+1); char **endptr = NULL; int number = strtol(operandCpy, endptr, 10); return number; @@ -97,9 +96,8 @@ void generateBranchOperands(a64inst_instruction *instr, char* opcode, char *oper break; case a64inst_CONDITIONAL: { - char *condition = NULL; - condition = strcpy(condition, opcode); - condition += 2; + char condition[strlen(opcode)+1]; + strcpy(condition, opcode+2); if(strcmp(condition, "eq")==0){ instr->data.BranchData.processOpData.conditionalData.cond = EQ; } else if (strcmp(condition, "ne")==0){ @@ -223,8 +221,8 @@ void parser_instruction(char asmLine[], a64inst_instruction *instr) { //type is label //add to symbol table instr->type = a64inst_LABEL; - char *opcodeCpy = NULL; - opcodeCpy = strcpy(opcodeCpy, opcode); + char opcodeCpy[strlen(opcode)+1]; + strcpy(opcodeCpy, opcode); char *labelData = strtok(opcodeCpy, ":"); instr->data.LabelData.label = labelData; } else { From 06b18706ed5a8ea68de68ce0ce87cdcf7b8bbf45 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Wed, 12 Jun 2024 17:34:14 +0100 Subject: [PATCH 3/3] rewrite opcode/operand splitting logic --- src/parser.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/parser.c b/src/parser.c index 4010764..432e926 100644 --- a/src/parser.c +++ b/src/parser.c @@ -142,7 +142,6 @@ void classifyOpcode(char* opcode, a64inst_instruction *instr, char *operandList[ } else { instr->data.BranchData.BranchType = a64inst_CONDITIONAL; } - generateBranchOperands(instr, opcode, operandList); } else if(isLoad == 0 || isStore == 0){ //loading/storing instruction; classify operands char *address = operandList[1]; @@ -165,7 +164,6 @@ void classifyOpcode(char* opcode, a64inst_instruction *instr, char *operandList[ //offset is literal, use symbol table and calculate difference } } - generateLoadStoreOperands(instr, opcode, operandList, numOperands); } else { if(classifyDPInst(operandList)){ @@ -209,9 +207,13 @@ void parser_instruction(char asmLine[], a64inst_instruction *instr) { //duplicated as strtok modifies the input string char stringptr[strlen(asmLine) + 1]; strcpy(stringptr, asmLine); - - char *opcode = strtok(stringptr, " "); - char *operands = strtok(stringptr, ""); + char *token; + token = strtok(stringptr, " "); + char opcode[strlen(token)+1]; + strcpy(opcode, token); + token = strtok(NULL, ""); + char operands[strlen(token)+1]; + strcpy(operands, token); if(strcmp(opcode, ".int") == 0){ //type is directive @@ -228,7 +230,7 @@ void parser_instruction(char asmLine[], a64inst_instruction *instr) { } else { //type is instruction int operandCount = 0; - char *operandList[4]; + char *operandList[5]; //generate list of operands tokeniseOperands(operands, &operandCount, operandList, &numOperands); //categorise instruction type from opcode and operands