Skip to content

Commit

Permalink
Merge pull request #117 from hiperiondev/main
Browse files Browse the repository at this point in the history
Add prefix in function for label code
  • Loading branch information
Ratstail91 authored Sep 1, 2023
2 parents 9dc9316 + edb5a52 commit b77f0fb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
9 changes: 6 additions & 3 deletions tools/disassembler/disassembler.c
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,8 @@ void disassemble(const char *filename, options_t config) {

if (!strcmp(litf->fun, "MAIN")) {
printf("MAIN:\n");
printf("%s", litf->str);
printf("%s", str_replace_substr_all(litf->str, ".lit FUNCTION ", ".lit FUNCTION (code=FUN_) "));

dis_disassemble_section(&prg, prg->pc, prg->len, 0, false, config);
free(litf->fun);
free(litf->str);
Expand All @@ -915,8 +916,10 @@ void disassemble(const char *filename, options_t config) {
continue;
}

printf("FUNCTION_%s:\n", litf->fun);
printf("%s", litf->str);
printf("FUN_%s:\n", litf->fun);
char sbtr[strlen(litf->fun) + 19];
sprintf(sbtr, ".lit FUNCTION (code=FUN_%s_) ", litf->fun);
printf("%s", str_replace_substr_all(litf->str, ".lit FUNCTION ", sbtr));

queue_node_t *fqf = function_queue_front;
while (fqf != NULL) {
Expand Down
32 changes: 32 additions & 0 deletions tools/disassembler/disassembler_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,35 @@ void str_append(char **str, const char *app) {
*str = realloc(*str, (strlen(*str) + strlen(app) + 1) * sizeof(char));
memcpy((*str) + strlen(*str), app, strlen(app) + 1);
}

char* str_replace_substr_all(char *mainstr, char *substr, char *newstr) {
int lenmain, lensub, i, j, lennew, startindex = -1, c;
lenmain = strlen(mainstr);
lensub = strlen(substr);
lennew = strlen(newstr);
char *result = (char*) malloc(sizeof(char) * (lenmain + 200));
for (c = 0, i = 0; i < lenmain; i++) {
if (lenmain - i >= lensub && *(mainstr + i) == *(substr)) {
startindex = i;
for (j = 1; j < lensub; j++)
if (*(mainstr + i + j) != *(substr + j)) {
startindex = -1;
break;
}
if (startindex != -1) {
for (j = 0; j < lennew; j++, c++) {
*(result + c) = *(newstr + j);
}
i = i + lensub - 1;
} else {
*(result + c) = *(mainstr + i);
c++;
}
} else {
*(result + c) = *(mainstr + i);
c++;
}
}
*(result + c) = '\0';
return result;
}
1 change: 1 addition & 0 deletions tools/disassembler/disassembler_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ void dis_enqueue(void *x, queue_node_t **queue_front, queue_node_t **queue_rear,
void dis_dequeue(queue_node_t **queue_front, queue_node_t **queue_rear, uint32_t *len);

void str_append(char **str, const char *app);
char* str_replace_substr_all(char *mainstr, char *substr, char *newstr);

#endif /* UTILS_H_ */

0 comments on commit b77f0fb

Please sign in to comment.