-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmips-parser.c
More file actions
113 lines (98 loc) · 3.73 KB
/
Copy pathmips-parser.c
File metadata and controls
113 lines (98 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "mips-parser.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int extractRegisterNumber(char *reg) {
// Handle $0, $1, etc.
if (reg[1] >= '0' && reg[1] <= '9') {
int regNum = atoi(®[1]);
// Bounds check: valid MIPS registers are 0-31
if (regNum < 0 || regNum > 31) {
return 0; // Return $0 if out of bounds
}
return regNum;
}
// Handle $zero, $at, $v0, $a0, $t0, $s0, etc.
if (strcmp(reg, "$zero") == 0) return 0;
if (strcmp(reg, "$at") == 0) return 1;
if (strcmp(reg, "$v0") == 0) return 2;
if (strcmp(reg, "$v1") == 0) return 3;
if (strcmp(reg, "$a0") == 0) return 4;
if (strcmp(reg, "$a1") == 0) return 5;
if (strcmp(reg, "$a2") == 0) return 6;
if (strcmp(reg, "$a3") == 0) return 7;
if (strcmp(reg, "$t0") == 0) return 8;
if (strcmp(reg, "$t1") == 0) return 9;
if (strcmp(reg, "$t2") == 0) return 10;
if (strcmp(reg, "$t3") == 0) return 11;
if (strcmp(reg, "$t4") == 0) return 12;
if (strcmp(reg, "$t5") == 0) return 13;
if (strcmp(reg, "$t6") == 0) return 14;
if (strcmp(reg, "$t7") == 0) return 15;
if (strcmp(reg, "$s0") == 0) return 16;
if (strcmp(reg, "$s1") == 0) return 17;
if (strcmp(reg, "$s2") == 0) return 18;
if (strcmp(reg, "$s3") == 0) return 19;
if (strcmp(reg, "$s4") == 0) return 20;
if (strcmp(reg, "$s5") == 0) return 21;
if (strcmp(reg, "$s6") == 0) return 22;
if (strcmp(reg, "$s7") == 0) return 23;
if (strcmp(reg, "$t8") == 0) return 24;
if (strcmp(reg, "$t9") == 0) return 25;
if (strcmp(reg, "$k0") == 0) return 26;
if (strcmp(reg, "$k1") == 0) return 27;
if (strcmp(reg, "$gp") == 0) return 28;
if (strcmp(reg, "$sp") == 0) return 29;
if (strcmp(reg, "$fp") == 0) return 30;
if (strcmp(reg, "$ra") == 0) return 31;
return 0; // Default to $0 if unknown
}
int parseImmediate(char *imm, int *isLabel) {
*isLabel = 0;
// Check if it's a label (starts with letter or contains non-digit chars besides '-')
int i = 0;
if (imm[0] == '-') i = 1; // Skip negative sign
int hasNonDigit = 0;
for (; imm[i] != '\0'; i++) {
if (!isdigit(imm[i])) {
hasNonDigit = 1;
break;
}
}
if (hasNonDigit) {
*isLabel = 1;
return 0; // Return placeholder offset for labels
}
return atoi(imm);
}
void parseMipsInstruction(char *mips, ParsedInstruction *output) {
char temp[128];
strncpy(temp, mips, sizeof(temp) - 1);
temp[sizeof(temp) - 1] = '\0'; // Ensure null termination
// Extract instruction (first token)
char *token = strtok(temp, " ,()");
// Check for null pointer before using token
if (token == NULL) {
output->instruction[0] = '\0';
output->numOperands = 0;
output->isLabel = 0;
return;
}
strncpy(output->instruction, token, sizeof(output->instruction) - 1);
output->instruction[sizeof(output->instruction) - 1] = '\0'; // Ensure null termination
output->numOperands = 0;
output->isLabel = 0;
// Extract operands
while ((token = strtok(NULL, " ,()")) != NULL && output->numOperands < MAX_OPERANDS) {
strncpy(output->operands[output->numOperands], token, sizeof(output->operands[output->numOperands]) - 1);
output->operands[output->numOperands][sizeof(output->operands[output->numOperands]) - 1] = '\0'; // Ensure null termination
output->numOperands++;
}
// Check if last operand is a label (for branch instructions)
if (output->numOperands > 0) {
char *lastOp = output->operands[output->numOperands - 1];
if (lastOp[0] != '$' && lastOp[0] != '-' && !isdigit(lastOp[0])) {
output->isLabel = 1;
}
}
}